[R] randomly select duplicated entries

Marc Schwartz marc_schwartz at comcast.net
Wed Jul 9 22:52:35 CEST 2008


on 07/09/2008 02:17 PM Juliet Hannah wrote:
> Using this data as an example
> 
> dat <- read.table(textConnection("Id         myvar
> 12 1
> 12 2
> 12 6
> 34 9
> 34 4
> 34 8
> 65 15
> 65 23"), header = TRUE)
> closeAllConnections()
> 
> how can I create another data set that does not have duplicate entries
> for 'Id', but the included values
> are randomly selected from the available ones.
> 
> Thanks!
> 
> Juliet


 > aggregate(dat$myvar, list(dat$Id), sample, 1)
   Group.1  x
1      12  6
2      34  4
3      65 15

 > aggregate(dat$myvar, list(dat$Id), sample, 1)
   Group.1  x
1      12  2
2      34  9
3      65 15

 > aggregate(dat$myvar, list(dat$Id), sample, 1)
   Group.1  x
1      12  1
2      34  8
3      65 23


HTH,

Marc Schwartz



More information about the R-help mailing list