title: How to sample data based off the distribution of another dataset in R
body: <p>I would like to sample a large dataset based on the distribution of a smaller dataset in R. I have been searching for a solution for some time without success. I am relatively new in R so I apologize if this is straightforward. However, I have tried some <a href="https://stackoverflow.com/questions/28388297/sampling-based-on-frequency-in-r">solutions</a>.</p>
  
  <p>Here are some sample data. I'll call it observed and model:</p>
  
  <pre><code># Set seed
  set.seed(2)
  
  # Create smaller observed data
  Obs &lt;- rnorm(1000, 5, 2.5)
  
  # Create larger modeled data
  set.seed(2)
  Model &lt;- rnorm(10000, 8, 1.5)
  </code></pre>
  
  <p>The distributions of the two datasets are as follows:
  <a href="https://i.stack.imgur.com/UqG9h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UqG9h.png" alt="enter image description here"></a></p>
  
  <p>Goal: I would like to sample the larger "model" dataset to match the smaller "observed". I understand that there are different data points involved so it won't be a direct match.</p>
  
  <p>I have been reading up on the <code>density()</code> and <code>sample()</code> where I do the following:</p>
  
  <pre><code># Obtain the density of the observed at the length of the model.
  # Note: info on the sample() function stated the prob argument in the sample() function 
  # must be the same length as what's being sampled. Thus, n=length(Model) below.
  
  dens.obs &lt;- density(Obs, n=length(Model))
  
  # Sample the Model data the length(Obs) at the probability of density of the observed
  set.seed(22)
  SampleMod &lt;- sample(Model, length(Obs), replace=FALSE, prob=dens.obs$y)
  </code></pre>
  
  <p>This gives me the new plot that looks very similar to the old (except for the tails):
  <a href="https://i.stack.imgur.com/n0tPA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n0tPA.png" alt="enter image description here"></a> </p>
  
  <p>I was hoping for a better match. Therefore I started explored using the density function on the model data. See below:</p>
  
  <pre><code># Density function on model, length of model
  dens.mod &lt;- density(Model, n=length(Model))
  
  # Sample the density of the model $x at the density of the observed $ y
  set.seed(22)
  SampleMod3 &lt;- sample(dens.mod$x, length(Obs), replace=FALSE, prob=dens.obs$y)
  </code></pre>
  
  <p>Here are two plots, the first is the same as the first sampled and the second is the second sampled:
  <a href="https://i.stack.imgur.com/O5HBO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O5HBO.png" alt="enter image description here"></a></p>
  
  <p>There is a more desirable shift in the right plot, which represents the sampled density of the modeled by the density of the observed. However, the data are not the same. That is, I did NOT sample the Modeled data. See below:</p>
  
  <pre><code>summary(SampleMod3 %in% Model)
  </code></pre>
  
  <p>produces:</p>
  
  <pre><code>   Mode   FALSE    NA's 
  logical    1000       0 
  </code></pre>
  
  <p>Indicating that I did not sample the modeled data, but rather the density of the modeled data. Is it possible to sample a dataset based on the distribution of another dataset? Thank you in advance.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Thanks for all the help guys! Here is my plot using <code>approxfun()</code> function offered from danielson and supported by bethanyp.</p>
  
  <p><a href="https://i.stack.imgur.com/oOZo5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oOZo5.png" alt="enter image description here"></a></p>
  
  <p>Any help with understanding why the funky new distribution?</p>