Ko-Kang Kevin Wang wrote:> I've been looking through the documentation for sample(), but can only get > it to work with vectors. Is it possible to sample from a dataframe?Do you want to sample rows from a single dataframe? How about something like this where 10 is the number of rows sampled: mydata[sample(dim(mydata)[1], 10),] hope it helps, Chuck Cleland
What do you want? The following selects 3 rows at random from DataFrame: DataFrame <- data.frame(x=1:9, y=rnorm(9)) DataFrame[sample(dim(DataFrame)[1], 3, replace=TRUE), ] Spencer Graves Ko-Kang Kevin Wang wrote:> Hi, > > I've been looking through the documentation for sample(), but can only get > it to work with vectors. Is it possible to sample from a dataframe? >
Hi, I've been looking through the documentation for sample(), but can only get it to work with vectors. Is it possible to sample from a dataframe? -- Cheers, Kevin ------------------------------------------------------------------------------ /* Time is the greatest teacher, unfortunately it kills its students */ -- Ko-Kang Kevin Wang Master of Science (MSc) Student SLC Tutor and Lab Demonstrator Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 Ph: 373-7599 x88475 (City) x88480 (Tamaki)
On Sat, 5 Apr 2003 09:01:13 +1200 (NZST), Ko-Kang Kevin Wang <kwan022 at stat.auckland.ac.nz> wrote:> Hi, > > I've been looking through the documentation for sample(), but can only > get it to work with vectors. Is it possible to sample from a dataframe? >I think somebody already answer, but, just in case: suppose dfrme is a "data frame" and "n" is the sample size, then using indexing you try this: samp1<-dfrme[sample(1:dim(dfrme)[1],n),] If you want to preserve the original order in the sample: samp1<-dfrme[sort(sample(1:dim(dfrme)[1],n)),] --