[R] how to select random rows ?

Liaw, Andy andy_liaw at merck.com
Mon Oct 27 16:43:21 CET 2003


Like this:

a.subset <- a[sample(nrow(a), how.many.I.want), ]

HTH,
Andy

> -----Original Message-----
> From: ryszard.czerminski at pharma.novartis.com 
> [mailto:ryszard.czerminski at pharma.novartis.com] 
> Sent: Monday, October 27, 2003 10:26 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] how to select random rows ?
> 
> 
> How can I select random subsets (rows!) from a data set ?
> 
> If I generate simple data set
> 
> > a <- data.frame(x=1:2, y = NaN, z = 2:1)
> > a
>   x   y z
> 1 1 NaN 2
> 2 2 NaN 1
> 
> I can select random subsets (colums) very easily using sample 
> function:
> 
> > sample(a, 2)
>   z   y
> 1 2 NaN
> 2 1 NaN
> 
> I expected that using transpose of a would do the same for 
> rows, but I am 
> getting
> rather unexpected outcome
> 
> > sample(t(a), 1)
> <NA>
>    1
> 
> R
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
>




More information about the R-help mailing list