[R] Randomly splitting a data frame in half
d.rizopoulos at erasmusmc.nl
Thu Mar 19 19:17:20 CET 2009
well, you need to keep track of the rows you sampled, e.g.,
dat <- data.frame(x = rnorm(20), y = rnorm(20), w = rnorm(20))
ii <- seq_len(nrow(dat))
ind1 <- sample(ii, 10)
ind2 <- ii[!ii %in% ind1]
I hope it helps.
Christopher David Desjardins wrote:
> I have a data frame in long format and I would like to randomly divide
> this data frame in half. The data frame consists of 39622 rows and I
> initially tried ...
> randomsample1 <- data[sample(nrow(data),19811), ]
> Where allows me to randomly select half of the rows and assign them to
> randomsample1 but then I couldn't figure out how to select those rows
> that were not selected and assign them to randomsample2.
> Please cc me if you reply as I'm a digest subscriber.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
More information about the R-help