How do I create multiple flag columns based on multiple columns with NA Values using ifelse?

Apolo Reis

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
IceCreamToucan

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas: How do I assign values based on multiple conditions for existing columns?

How do I count specific values across multiple columns in pandas

How do I fill NA values in multiple columns in pandas?

How do I distributed a table using values from multiple columns?

How do I create a new column based on multiple conditions from multiple columns?

Selecting columns based on row values in multiple columns using dplyr

How do I create one column based on the names of multiple other columns in python?

How do I create a dummy variable that depends on values in multiple columns?

How do I SELECT multiple values from a function into separate columns?

How do I create multiple variables using ifelse based on matching names of other variables?

How do I return values from multiple columns when the column names are based on a variable result

How do I create a third column based on Character Values of other columns, excluding NA and values?

How to create multiple flag columns based on list values found in the dataframe column?

How do I sort sort a QTreeWidget or QTableWidget by multiple columns (and how do I sort these columns as numerical values)?

How do I convert multiple columns to individual rows/values in pandas?

How to aggregate multiple columns in a dataframe using values multiple columns

How to create a unique identifier based on multiple columns?

Create new column into dataframe based on values from other columns using apply function onto multiple columns

Using ifelse conditional on multiple columns

How to remove duplicate values based on multiple columns

How do I return multiple columns without consider Na values and group by other columns name in R?

How do I replace null values of multiple columns with values from multiple different columns

How to dynamically create columns based on multiple conditions

apply ifelse to multiple columns and create new column

How do i count values in multiple columns based on multiple criteria and create a new column row wise?

How do I query based on values in multiple columns

How do I fill in values of a column based on multiple columns in R?

Create Column based on multiple columns values

How do I create a new dataframe based on row values of multiple columns in python?