Hi,

I am trying to add a column of numbers to a data frame in R with multiple
conditions.

Here is a simplified example df:

[A]  [B] [C] [D] [E]
[1] 1 X 90 88
[2] 1 Y 72 70
[3] 1 Z 67 41
[4] 2 X 74 49
[5] 2 Y 42 50
[6] 2 Z 81 56
[7] 3 X 92 59
[8] 3 Y 94 80
[9] 3 Z 80 82

I would like column [E] to have a certain value (found either in [C] or
[D]) based on conditions in columns [A] *and* [B].

E.g. :
if [A] = 1 and [B] = X, then [E] = the entry in [C] for that row (i.e., 90)
if [A] = 1 and [B] = Y, then [E] = the entry in [D] for that row (i.e., 70)
if [A] = 1 and [B] = Z, then [E] = the entry in [C] for that row (i.e., 67)

if [A] = 2 and [B] = X, then [E] = the entry in [C] for that row (i.e., 74)
if [A] = 2 and [B] = Y, then [E] = the entry in [D] for that row (i.e., 50)
if [A] = 2 and [B] = Z, then [E] = the entry in [C] for that row (i.e., 81)

and so on.

ATTEMPT TO RESOLVE:

The following code allowed me to add values for column [E] when [A] ==1,
but I can't figure out how to keep the code going in order to get a value
for column [E] based on all of the numbers in column [A] and the secondary
condition for [B] ([A] goes from 1:48).

df$[E] <-


ifelse((df$A == 1) & (df$B == "X"), df$C[df$A == 1],

ifelse((df$A == 1) & (df$B == "Y"), df$D[df$A == 1],

ifelse((df$A == 1) & (df$B == "Z"),  df$C[df$A == 1],

NA))))


Thank you for any advice you can give.


Libby G

libbymg[at]utexas.edu

        [[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.

Reply via email to