1
$\begingroup$

I am trying to create a comparison group. So far this group contains 45 data points and I need to populate the remaining 55 (for a total of 100 data points).

These remaining 55 need to be a randomized selection supplied from a larger data set. Any recommendations for R code that would create a randomization loop?

$\endgroup$
1
$\begingroup$

This will give you a sample with 55 record from the larger data-set that you have.

 sample <- df[sample(1:nrow(df), 55, replace=FALSE),]

If you want to make a reproducible example of the sample, you need to set seed like this:

 set.seed(57)
 sample <- df[sample(1:nrow(df), 55, replace=FALSE),]

So later you can use the same seed for getting the same result.

p.s. replace=FALSE means that each record after that has been picked from the df to be included in the sample, will be excluded from the df and it will not be chosen multiple times.

| improve this answer | |
$\endgroup$
  • $\begingroup$ thank you - is there any additional way to ensure that the random 55 data points that are pulled are matched for a certain variable with the 45 data points that already exist? For example, say Im trying to match a list of participants on years of education - is there a way to randomize the selection of the 55 so that they match with the existing 45 data points? $\endgroup$ – timemachinez Apr 3 '17 at 20:55
  • $\begingroup$ @timemachinez What do you mean by "match"? Do you want 55-records to have same average or same value for "years of education" as the other 45-ones? $\endgroup$ – M-- Apr 3 '17 at 20:58
  • $\begingroup$ Id like them to be the same average, sorry for lack of clarification $\endgroup$ – timemachinez Apr 3 '17 at 21:05
  • $\begingroup$ @timemachinez Let me think about. But that's not random sampling since you are enforcing a condition. $\endgroup$ – M-- Apr 3 '17 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.