[R] split dataframe by sample()

2011-06-23 Thread Paul Tanger
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]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] split dataframe by sample()

2011-06-23 Thread Sarah Goslee
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.tan...@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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.