Search code examples
rnadplyracross

Mutate across with ifelse and is.na


I have data in the format outlined below, where all of the variables I need to work with are either NA or the name of the variable, and I need to change the NAs to 0 and the strings to 1. I'm trying to use dplyr::across() and ifelse(), but what gets returned is all 1s. The only solution I have right now that works is mutating each variable individually.

How would I change all NAs to 0 and all strings to 1 across multiple variables at once?

library(dplyr)
color_names <- c("black", "grey", "white", "purple")

my_colors <- tribble(
  ~black, ~grey, ~white,  ~purple,
  NA,     "grey", NA,     "purple",
  NA,     NA,     "white", NA,
  "black",NA,     NA,      NA,
  NA,     "grey",  NA,     NA
)

my_colors %>%
  mutate(across(all_of(color_names), ~ifelse(is.na(cur_column()), 0, 1)))
#> # A tibble: 4 x 4
#>   black  grey white purple
#>   <dbl> <dbl> <dbl>  <dbl>
#> 1     1     1     1      1
#> 2     1     1     1      1
#> 3     1     1     1      1
#> 4     1     1     1      1

Created on 2021-01-13 by the reprex package (v0.3.0)


Solution

  • I guess you could do it like this:

    library(tidyverse)
    color_names <- c("black", "grey", "white", "purple")
    
    my_colors <- tribble(
      ~black, ~grey, ~white,  ~purple,
      NA,     "grey", NA,     "purple",
      NA,     NA,     "white", NA,
      "black",NA,     NA,      NA,
      NA,     "grey",  NA,     NA
    )
    
    my_colors %>% 
      mutate(across(.cols = everything(),
                    .fns = ~ ifelse(is.na(.x) == TRUE, 0, 1)))
    
    # A tibble: 4 x 4
      black  grey white purple
      <dbl> <dbl> <dbl>  <dbl>
    1     0     1     0      1
    2     0     0     1      0
    3     1     0     0      0
    4     0     1     0      0