[R] Select random observation from a group

Peter Alspach Peter.Alspach at plantandfood.co.nz
Fri Mar 28 02:50:10 CET 2014


Tena koe Whitney

tapply should work.  Try:

tapply(yourData$IndividualID, yourData$FamilyID, sample, size=1) # untested

HTH ....

Peter Alspach

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Whitney Melroy
Sent: Friday, 28 March 2014 10:11 a.m.
To: r-help at r-project.org
Subject: [R] Select random observation from a group

Hello, 

I have a dataset with family data. For an analysis, I need to select one subject per family at random. 

Here is an example of what my data look like: 

FamilyID IndividualID          DadID    MomID      Sex
1		 101			103		104		    1
1		 102			103		104		    2
1		 103			0		0		    1
1		 104			0		0		    2
2		 201			202		203		    1
2		 202			202		203		    2
2		 203			202		203		    1
2		 204			202		203		    2

I want to randomly select ONE subject for each family (there are roughly 2300 families) and make a new dataframe. 

Here is what I tried so far, with no success: 


Uniq.fam.id<-df[unique(df$FamilyID),]

Uniq.fam.id <- df[sample(unique(df$FamilyID)),]


Uniq.fam<-unique(df$FamilyID)
Uniq.fam.id <- df[sample(Uniq.fam),]

I would be eternally grateful for any help. 

Thanks, 

Whitney 
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
The contents of this e-mail are confidential and may be ...{{dropped:14}}




More information about the R-help mailing list