Re: [R] creating a new variable, conditional on the value of an existing variable, selected conditionally

From: Erik Iverson <eriki_at_ccbr.umn.edu>
Date: Wed, 09 Jun 2010 11:55:20 -0500

Can your data.frame be properly coerced to a matrix like your example?

If so,

apply(f, 1, function(x) x[eval(x)["D"]])

Malcolm Fairbrother wrote:
> Dear all,
>
> I have a data frame f, with four variables:
>
> f <- data.frame(A=c(0,0,1,1), B=c(0,1,0,1), C=c(1,1,0,1), D=c(3,1,2,3))
> f
> A B C D
> 1 0 0 1 3
> 2 0 1 1 1
> 3 1 0 0 2
> 4 1 1 1 3
>
> I want to create a new variable (f$E), such that each of its elements is drawn from either f$A, f$B, or f$C, according to the value (for each row) of f$D (values of which range from 1 to 3).
>
> In the first row, D is 3, so I want the value from the third variable (C), which for the first row is 1. In the second row, D is 1, so I want the value from the first variable (A), which for the second row is 0. And so forth, such that in the end my new data frame looks like:
>
> A B C D E
> 1 0 0 1 3 1
> 2 0 1 1 1 0
> 3 1 0 0 2 0
> 4 1 1 1 3 1
>
> My question is: How do I do this for a much larger dataset, where my "index variable" (f$D in this example) actually indexes a much larger number of variables (not just three)?
>
> I know that in principle I could do this with a long series of nested ifelse statements (as below), but I assume there is some less cumbersome option, and I'd like to know what it is. Any help would be much appreciated. Apologies if I'm missing something obvious.
>
> f$E <- ifelse(f$D==3, f$C, ifelse(f$D==2, f$B, f$A))
>
> Thanks,
> Malcolm
>
> ______________________________________________
> 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.



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 Wed 09 Jun 2010 - 16:57:17 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 Wed 09 Jun 2010 - 17:00:28 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