ryszard.czerminski@pharma.novartis.com
2003-Oct-27 15:25 UTC
[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) > ax 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]]
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 >
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 > >