Re: [R] simple if question

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sat, 26 Mar 2011 19:12:21 -0400

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



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. Received on Sat 26 Mar 2011 - 23:15:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 26 Mar 2011 - 23:20:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive