I want to create one additional called new column from d,e variables condition on a, b and c columns, but I am struggling to code all these condition in a single function:
#Condition:
new = e if a=2, b=2 & c=1
new = d if a=2, b=1 & c=2
new = e if a=2, b=2 & c=2
#data:
df <- data.frame (a = c(2,2,1,2,1,1),
b = c(2,1,1,2,2,2),
c = c(1,1,2,2,1,2),
d = c(1,3,5,2,7,5),
e = c(4,5,1,7,3,5))
CodePudding user response:
Assuming ,
means AND:
data.frame(
a = c(2, 2, 1, 2, 1, 1),
b = c(2, 1, 1, 2, 2, 2),
c = c(1, 1, 2, 2, 1, 2),
d = c(1, 3, 5, 2, 7, 5),
e = c(4, 5, 1, 7, 3, 5)
) %>%
mutate(
new = case_when(
a == 2 & b == 2 & c == 1 ~ e,
a == 2 & b == 1 & c == 2 ~ d,
a == 2 & b == 2 & c == 2 ~ e,
TRUE ~ NA_real_
)
)
Note that the output is:
a b c d e new
1 2 2 1 1 4 4
2 2 1 1 3 5 NA
3 1 1 2 5 1 NA
4 2 2 2 2 7 7
5 1 2 1 7 3 NA
6 1 2 2 5 5 NA
since the condition does not cover all the cases.
CodePudding user response:
A possible solution:
library(dplyr)
df %>%
mutate(new = case_when(a == 2 & b == 2 & c == 1 ~ e,
a == 2 & b == 1 & c == 2 ~ d,
a == 2 & b == 2 & c == 2 ~ e))
#> a b c d e new
#> 1 2 2 1 1 4 4
#> 2 2 1 1 3 5 NA
#> 3 1 1 2 5 1 NA
#> 4 2 2 2 2 7 7
#> 5 1 2 1 7 3 NA
#> 6 1 2 2 5 5 NA
CodePudding user response:
data.table
option using fifelse
:
library(data.table)
setDT(df)[, new := fifelse(a == 2 & b == 2 & c == 1, e,
fifelse(a == 2 & b == 1 & c == 2, d,
fifelse(a == 2 & b == 2 & c == 2, e, NA_real_)))]
Output:
a b c d e new
1: 2 2 1 1 4 4
2: 2 1 1 3 5 NA
3: 1 1 2 5 1 NA
4: 2 2 2 2 7 7
5: 1 2 1 7 3 NA
6: 1 2 2 5 5 NA