I am trying to create a new column based on specific values in several (four) other columns.
Reprex:
col1 <- c("a", "", "a")
col2 <- c("", "b", "b")
df <- data.frame(col1, col2)
I have tried with case_when, but cannot get it to apply to all values.
df$col3 <- with(df, case_when(col1 %in% c("a") & col2 %in% c("b") ~ "c" ))
What I want to achieve:
| col1| col2 | col3 |
|:--: | :-------:|:-------:|
| a | | a |
| | b | b |
| a | b | c |
So if col1 is a, col2 is na then col 3 is a.
If col1 is na, col2 b then col3 should be b and finally if col1 is a, col2 b then col3 should be c
Copyright Notice:Content Author:「Troels」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/72047691/create-column-based-on-specific-values-in-other-columns