[R] how to select random rows ?

Henrik Bengtsson hb at maths.lth.se
Mon Oct 27 16:55:16 CET 2003


rowIdx <- sample(nrow(a), size=nbrOfSamples)
a[rowIdx,]

and/or

colIdx <- sample(ncol(a), size=nbrOfSamples)
a[,colIdx]

/Henrik Bengtsson

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> ryszard.czerminski at pharma.novartis.com
> Sent: den 27 oktober 2003 16:26
> 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/mailma> n/listinfo/r-help
> 
>




More information about the R-help mailing list