Home > front end >  How to perform ifelse on multiple columns?
How to perform ifelse on multiple columns?

Time:07-06

I have sample dataframe

dat <- data.frame(
  variable1 = c(NA,2,3,4,5,6,99),
  variable2 = c(NA,2,99,4,5,6,7),
  variable3 = c(NA,2,3,4,5,6,7),
  variable4 = c(5:11),
  variable5 = c(1,2,3,4,5,6,999),
  variable6 = c(1,2,3,4,999,6,7),
  variable7 = c(1:7)
)

  variable1 variable2 variable3 variable4 variable5 variable6 variable7 
1        NA        NA        NA         5         1         1         1 
2         2         2         2         6         2         2         2 
3         3        99         3         7         3         3         3 
4         4         4         4         8         4         4         4
5         5         5         5         9         5       999         5
6         6         6         6        10         6         6         6
7        99         7         7        11       999         7         7 

I want to perform an ifelse saying if variable1 and variable2 and variable3 are NA, take variable4, variable5, variable6, otherwise take variable1, variable2, variable3 to the new columns variable 8, variable9, variable10.

so the new data frame should look like this

  variable1 variable2 variable3 variable4 variable5 variable6 variable7 variable8 variable9 variable10
1        NA        NA        NA         5         1         1         1         5         1         1
2         2         2         2         6         2         2         2         2         2         2 
3         3        99         3         7         3         3         3         3        99         3
4         4         4         4         8         4         4         4         4         4         4 
5         5         5         5         9         5       999         5         5         5         5
6         6         6         6        10         6         6         6         6         6         6
7        99         7         7        11       999         7         7         99        7         7

I prefer a dplyr solution ;)

CodePudding user response:

dplyr::case_when is perfect for this:

library(dplyr)
dat %>% 
  mutate(across(all_of(1:7), ~as.numeric(.))) %>% #important for all data to be numeric
  mutate(variable8 = case_when(is.na(variable1) & is.na(variable2) & is.na(variable3) ~ variable4,
                               TRUE ~ variable1)) %>%
  mutate(variable9 = case_when(is.na(variable1) & is.na(variable2) & is.na(variable3) ~ variable5,
                               TRUE ~ variable2)) %>%
  mutate(variable10 = case_when(is.na(variable1) & is.na(variable2) & is.na(variable3) ~ variable6,
                               TRUE ~ variable3))

  variable1 variable2 variable3 variable4 variable5
1        NA        NA        NA         5         1
2         2         2         2         6         2
3         3        99         3         7         3
4         4         4         4         8         4
5         5         5         5         9         5
6         6         6         6        10         6
7        99         7         7        11       999
  variable6 variable7 variable8 variable9 variable10
1         1         1         5         1          1
2         2         2         2         2          2
3         3         3         3        99          3
4         4         4         4         4          4
5       999         5         5         5          5
6         6         6         6         6          6
7         7         7        99         7          7

CodePudding user response:

Another possible solution:

library(tidyverse)

dat %>% 
  mutate(pmap_dfr(., ~ (if (all(is.na(c(...)[1:3]))) {c(...)[4:6]} else 
    {c(...)[1:3]}) %>% set_names(str_c("variable", 8:10))))

#>   variable1 variable2 variable3 variable4 variable5 variable6 variable7
#> 1        NA        NA        NA         5         1         1         1
#> 2         2         2         2         6         2         2         2
#> 3         3        99         3         7         3         3         3
#> 4         4         4         4         8         4         4         4
#> 5         5         5         5         9         5       999         5
#> 6         6         6         6        10         6         6         6
#> 7        99         7         7        11       999         7         7
#>   variable8 variable9 variable10
#> 1         5         1          1
#> 2         2         2          2
#> 3         3        99          3
#> 4         4         4          4
#> 5         5         5          5
#> 6         6         6          6
#> 7        99         7          7
  • Related