[R] Select random observation from a group

Whitney Melroy wmelroy827 at gmail.com
Thu Mar 27 22:10:36 CET 2014


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 


More information about the R-help mailing list