# Thread: Random Resampling from Nest Data

1. ## Random Resampling from Nest Data

First off, thanks for your help if you have any comments, and I apologize if there is a post about this somewhere (I was unable to find one).

So, here is my problem. I need to randomly draw samples from my dataset of grouped data. I have observations in separate rows. Each participant belongs to both of two groups (data drawn at separate times) and has numerous observations (100 or so repeated-measures observations; so each participant has data on about 100 or so rows). I need to randomly draw two observations from each participant within each group. Then I will statistically evaluate those data. Then I will draw four observations from participant from each group. Repeat analysis. Then six, eight, etc. (It's a reliability study).

So, the problem I have is randomly drawing a subsample from among the 100 or so observations of a participant in a group. Does anyone have any idea how I could accomplish this in R. I hope I explained this well; let me know if you need more details.

Best,
Peter

2. ## Re: Random Resampling from Nest Data

Could you provide a small example of your data? The function 'dput' might help you out with this.

3. ## Re: Random Resampling from Nest Data

I'd be happy to. I hope this helps.

Participant<-ID
Time<-Time point

I've attached a .csv file. I just included a sample of 2 participants for size reasons.

Best

---

Code:
``````testmatrix <- structure(list(Participant = c(480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L, 480L,
480L, 480L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L, 485L,
485L, 485L, 485L, 485L, 485L, 485L), Time = c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), AdpMeanAmp = c(-9.754203056, -0.495155278, -2.195121944,
1.41495425, -6.078787278, 1.346799722, 0.643207778, -15.4532,
0.667331389, -4.870593611, -3.209394444, -3.207669444, -0.929003167,
-2.322763611, -1.296921389, 3.518718889, 5.355625, -0.918338889,
-14.85672222, -6.038286667, -2.475296722, -2.604885278, -2.555607056,
5.275308333, -2.008800833, -9.691686342, -2.302332833, -1.5296975,
-3.321189722, -16.96612778, -8.092960889, -1.811618722, 5.098173056,
4.976565833, 3.345711944, 2.341464722, -1.176499722, -6.47635,
-16.04515556, 2.6965525, 1.342603197, 5.492311111, 1.279819056,
5.013305111, 0.977263333, -14.95686667, -0.472862833, 1.443284222,
-1.187473611, 0.067691667, 2.092128556, 2.4527825, -6.264150556,
-3.402644333, -4.981451667, -0.683365556, 3.637192778, 10.99151278,
2.326602111, 1.598646667, -0.250853056, 0.384139444, -6.602626028,
-10.41632908, 2.315691667, -0.527079778, -8.908269167, -6.391730556,
0.619586389, -9.358659444, -3.740466278, -11.74775556, 5.660477778,
-0.299126667, -4.009585028, 4.333248056, -1.883224278, 3.810443611,
0.97681375, -0.70781625, -4.047147222, -6.918295833, 4.204295556,
6.691665278, 3.022944472, -3.671905, 0.274677833, 4.185016583,
-5.447118889, 0.563756944, -5.773984722, -3.017856667, 4.340719444,
0.245574186, -5.858672222, -4.307608333, -7.513298889, 3.455451389,
3.627268611, -2.989485278, -3.647620556, 6.773490556, 1.754427139,
-0.008031722, 0.279959778, -2.354376389, -2.78783425, -4.001241389,
-4.180244167, 0.291680944, 11.21774722, 3.2920975, -0.640009444,
-9.894127108, -9.379955944, 2.2892225, -10.48888056, -2.548196111,
-1.801460278, 2.33685175, -5.136521389, -9.777996667, 2.74887,
9.425130556, -8.950200556, -1.342131719, 1.229002778, -1.429317103,
4.504315833, 4.5956925, -5.520082222, 2.722141944, 5.467383333,
0.901795556, 3.968634167, -8.456018889, 10.144125, 3.559389167,
2.2215925, 6.922958333, -12.50711111, -0.942239444, -3.135135139,
-5.995455556, -3.319592222, -0.619252944, -1.060613194, -6.642757222,
-0.438860278, -6.192648889, 9.246641667, 0.314120306, 1.417104361,
-4.245389444, 3.700203389, -7.864175, -3.160018167, -1.442484972,
-1.724199972, 0.257440111, -2.894643333, -2.731235, -12.68484722,
-0.949738056, 2.719832528, -1.778345333, -10.17781389, -0.066781972,
2.715815278, -0.430233444, -7.484216667, -2.719353667, 5.186274167,
-5.860130556, -5.951364722, -6.459845833, -2.885905833, 0.378856069,
-7.436405833, 9.127244444, -2.920192778, -1.681440833, -8.517983333,
-9.233713889, 1.85049875, -8.616461667, -2.689207778, -5.694225,
0.122773378, -5.973558333, 2.271115278, -1.250881389, -2.554280333,
-4.398802528, 2.139071889, 0.682897417, 2.626935186, -0.295735,
8.238261111, -2.882626972, -6.961102778, -18.93644444, -5.240122222,
-9.332697222, 6.102175, 0.151331083, -0.949147778, -0.249562889,
-0.772839722, -1.024065611, -6.208188889, 8.204308333, -2.671645139,
0.35774975, 2.044862417, 1.084563806, -2.973022972, -10.93395278,
-3.793119444, -7.836432222, -0.681975778, -1.979107222, -10.81266944,
-5.085684722, -3.207681917, -7.029186944, 2.325443611, -3.830519444,
4.288383889, -6.078538889, 3.090574444, -18.96408333, 0.689698917,
0.02526375, 1.099248583, -13.94686944, 3.142726944, 2.116814725,
1.023272778, 1.269933389, 4.860541667, -2.996058528, -3.843702778,
4.155211583, -0.789725278, 0.596395472, -3.649456944, 4.371523889,
-1.994977778, -0.206682278, 1.856320833, -1.219384139, 4.802146944,
0.015093333, -2.896420556, -6.378116667, -8.245214861, 5.217671667,
-9.632105556, -0.355930056, -9.134325, -6.103533333, -1.977648056,
-2.015617694)), .Names = c("Participant", "Time", "AdpMeanAmp"
), class = "data.frame", row.names = c(NA, -264L))``````

4. ## Re: Random Resampling from Nest Data

ddply from the plyr package could make this a relatively easy task. I wasn't sure if you wanted to sample from within each time period within each participant but that's what my code is doing.

Code:
``````library(plyr)

ddply(testmatrix, .(Participant, Time), summarize, out = sample(AdpMeanAmp, 2))``````
If you just want the sample to be from the Participant but don't care about time then change .(Participant, Time) to .(Participant) and to change how many samples you grab just change the 2 in sample(AdpMeanAmp, 2) to however many you want.

5. ## The Following User Says Thank You to Dason For This Useful Post:

pclayson (06-15-2012)

6. ## Re: Random Resampling from Nest Data

Brilliant! Thank you very much! It works perfectly.

I appreciate it!

Best,
Peter