Home > Back-end >  How do I create multiple flag columns based on multiple columns with NA Values using ifelse?
How do I create multiple flag columns based on multiple columns with NA Values using ifelse?

Time:02-18

I tried using this first:

for (i in names(data)){
  data[paste0('FLAG_NA_',i)]<- ifelse(is.na(data$i),1,0)
}

But this code only creates new columns with only NA values

I found a similar solution to what I want here: How to apply ifelse function across multiple columns and create new columns in R.

The answer is:

data %>%
  mutate(across(starts_with('C'), ~ifelse( .x == "Off", 1, 0), .names = 'scr_{sub("C", "", .col)}'))

But when I try to use the is.na() condition on the code, It doesn't work:

data %>%
  mutate(across(names(data), ~ifelse( .x %>% is.na, 1, 0), .names = paste0('FLAG_NA_',names(data))))

Error message:

Error: Problem with `mutate()` input `..1`.
i `..1 = across(...)`.
x All unnamed arguments must be length 1

CodePudding user response:

The .names in across should not be a vector. It should be a single character value that serves as a "glue specification" for the names using "{.col} to stand for the selected column name, and {.fn} to stand for the name of the function being applied". So in this case, you could use 'FLAG_NA_{.col}', producing the output below.

## Example data
set.seed(2022)
library(magrittr)
data <- 
  letters[1:3] %>% 
    setNames(., .) %>% 
    purrr::map_dfc(~ sample(c(1, NA, 3), 5, T))
  
data
#> # A tibble: 5 × 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     3     3     3
#> 2    NA    NA     1
#> 3     3     3    NA
#> 4     3     1     3
#> 5    NA    NA    NA

## Create new variables
library(dplyr, warn.conflicts = FALSE)

data %>%
  mutate(across(everything(), ~ as.numeric(is.na(.x)), 
         .names = 'FLAG_NA_{.col}'))
#> # A tibble: 5 × 6
#>       a     b     c FLAG_NA_a FLAG_NA_b FLAG_NA_c
#>   <dbl> <dbl> <dbl>     <dbl>     <dbl>     <dbl>
#> 1     3     3     3         0         0         0
#> 2    NA    NA     1         1         1         0
#> 3     3     3    NA         0         0         1
#> 4     3     1     3         0         0         0
#> 5    NA    NA    NA         1         1         1

Created on 2022-02-17 by the reprex package (v2.0.1)

  • Related