Hi everyone, I have just got different samples from a dataframe (independent and exclusive, there aren't common elements among them). I want to create a variable that indicate the sampling selection of the elements in the original dataframe (for example, 0 = no selected, 1= sample 1, 2=sample 2, etc.). I have tried to do it with ifelse command, but the problem is that the second line replaces the values of the first line, and I haven't been able to do it with the if command (I got this error: In if (data$school %in% sample1) { : the condition has length > 1 and only the first element will be used) data$selection <- ifelse(data$school %in% sample1, 1, 0) data$selection <- ifelse(data$school %in% sample2, 2, 0) Any ideas? Thank you in advance. -- Sebasti?n Daza sebastian.daza at gmail.com
On Mar 26, 2011, at 7:05 PM, Sebasti?n Daza wrote:> Hi everyone, > I have just got different samples from a dataframe (independent and > exclusive, there aren't common elements among them). I want to > create a variable that indicate the sampling selection of the > elements in the original dataframe (for example, 0 = no selected, 1= > sample 1, 2=sample 2, etc.). > > I have tried to do it with ifelse command, but the problem is that > the second line replaces the values of the first line, and I haven't > been able to do it with the if command (I got this error: In if (data > $school %in% sample1) { : > the condition has length > 1 and only the first element will be used) >Nested ifelse's should work. There may be a limit to nested depth. data$selection <- ifelse(data$school %in% sample1, 1, # else ifelse(data$school %in% sample2, 2, #else 0) )> > Any ideas? > Thank you in advance. > > -- > Sebasti?n Daza > sebastian.daza at gmail.com > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT
Posting some sample data would help, but I think something like this is what you want data[data$school=='Cornell University',] For example CO2[CO2$Type=='Quebec',] Tom 2011/3/26 Sebasti?n Daza <sebastian.daza at gmail.com>:> Hi everyone, > I have just got different samples from a dataframe (independent and > exclusive, there aren't common elements among them). I want to create a > variable that indicate the sampling selection of the elements in the > original dataframe (for example, 0 = no selected, 1= sample 1, 2=sample 2, > etc.). > > I have tried to do it with ifelse command, but the problem is that the > second line replaces the values of the first line, and I haven't been able > to do it with the if command (I got this error: In if (data$school %in% > sample1) { : > ?the condition has length > 1 and only the first element will be used) > > data$selection <- ifelse(data$school %in% sample1, 1, 0) > data$selection <- ifelse(data$school %in% sample2, 2, 0) > > Any ideas? > Thank you in advance. > > -- > Sebasti?n Daza > sebastian.daza at gmail.com > > ______________________________________________ > R-help at 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. >
Maybe Matching Threads
- computing marginal values based on multiple columns?
- How to Store the executed values in a dataframe & rle function
- grey scale graphs
- conditional filter resulting in 2 new dataframes
- phantom NA/NaN/Inf in foreign function call (or something altogether different?)