[R] split dataframe by sample()

Sarah Goslee sarah.goslee at gmail.com
Thu Jun 23 18:21:06 CEST 2011


It's very simple to do this in steps:

> # to make separate dataframes
> df <- data.frame(A=1:5, B=11:15)
> df.sample <- c(1,3,4)
> df[df.sample, ]
  A  B
1 1 11
3 3 13
4 4 14
> df[-df.sample, ]
  A  B
2 2 12
5 5 15
>
> # or a list with two components
> split(df, 1:nrow(df) %in% df.sample)
$`FALSE`
  A  B
2 2 12
5 5 15

$`TRUE`
  A  B
1 1 11
3 3 13
4 4 14

Sarah

On Thu, Jun 23, 2011 at 12:12 PM, Paul Tanger <paul.tanger at colostate.edu> wrote:
> Hi,
>
> I seemingly have a simple problem, but I've spend hours reading guides &
> posts on this forum and I can't seem to piece together what I need.
> I have a dataframe where I want to divide it into two subsets: a sample, and
> the remainder of the dataframe in a new frame.
> I've tried this:
>
> split(df, sample(nrow(df), size=100, replace=FALSE))
>
> another way would be to make a new dataframe of my sample and (something I
> can do in SQL but not R) then select rows that are NOT in the sample
> dataframe.
>
> Thanks for any help!
>
>        [[alternative HTML version deleted]]
>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list