Reputation: 47
I'd like to select and return a column value contained within one of a subset of columns, based on the value in another column.
Specifically, I'd like to create a new variable ("NEWE") by selecting the value in RE when the MAX variable = "RP" and by selecting the value in IE when the MAX variable = "IP".
Of note, I am working with a data table of ~160 columns and ~70,000 rows (I am noting the existence of additional columns in my example by including V1-V9).
Have:
V1 … V9 RE IE MAX
4 … 3 3 4 RP
6 … 6 3 2 IP
2 … 2 1 2 IP
6 … 2 2 3 RP
. . . . . .
. . . . . .
. . . . . .
And the data set I would like:
V1 … V9 RE IE MAX NEWE
4 … 3 3 4 RP 3
6 … 6 3 2 IP 2
2 … 2 1 2 IP 2
6 … 2 2 3 RP 2
. . . . . . .
. . . . . . .
. . . . . . .
Thanks in advance for your help!
Upvotes: 2
Views: 3242
Reputation: 8413
data$NEWE <- ifelse(data$MAX == "RP", data$RE, ifelse(data$MAX == "RP",data$IE, "value if both doesn't satify"))
if its either of RP and IP always!
data$NEWE <- ifelse(data$MAX == "RP", data$RE , data$IE)
for your case : its a simple extension of ifelse - (to avoid typo prefer using with())
data$NEWE <- with(data, ifelse(MAX == "RP", RE,
ifelse(MAX == "IP",IE,
ifelse(MAX == "AP", AE,
ifelse(MAX == "SP", SE,
ifelse(MAX == "EP",EE,
ifelse(MAX == "CP", CE, NA)))))))
Upvotes: 4