Home > Net >  how to recode dummy column with the column name?
how to recode dummy column with the column name?

Time:05-09

I have the input dataset, and I'm looking for generating the output dataset by recoding 1 as the name of the columns and 0 as NA. I managed to do it manually see Not optional solution below. But I have a dataset with hundreds of columns, so I'm looking for a way to automatize this process.

Packages

library(tibble)
library(dplyr)

Input

input <- tibble( a = c(1, 0, 0, 1, 0),
                 b = c(0, 0, 0, 1, 1),
                 c = c(1, 1, 1, 1, 1),
                 d = c(0, 0, 0, 0, 0))


# # A tibble: 5 × 4
#       a     b     c     d
#   <dbl> <dbl> <dbl> <dbl>
# 1     1     0     1     0
# 2     0     0     1     0
# 3     0     0     1     0
# 4     1     1     1     0
# 5     0     1     1     0

Output

output <- tibble( a = c("a", NA, NA, "a", NA),
                  b = c(NA, NA, NA, "b", NA),
                  c = c("c", "c", "c", "c", "c"),
                  d = c(NA, NA, NA, NA, NA))

   
# # A tibble: 5 × 4
#       a     b     c     d    
#   <chr> <chr> <chr> <lgl>
# 1 a     NA    c     NA   
# 2 NA    NA    c     NA   
# 3 NA    NA    c     NA   
# 4 a     b     c     NA   
# 5 NA    NA    c     NA 

Not optional solution

input %>% 
  mutate(a = case_when(a == 1 ~ "a",
                       T ~ NA_character_),
         b = case_when(b == 1 ~ "b",
                       T ~ NA_character_),
         c = case_when(c == 1 ~ "c",
                       T ~ NA_character_),
         d = case_when(d == 1 ~ "d",
                       T ~ NA_character_))

CodePudding user response:

We could use across with an ifelse statement:

library(dplyr)

input %>% 
  mutate(across(everything(), ~ifelse(. == 1, cur_column(), NA)))
  a     b     c     d    
  <chr> <chr> <chr> <lgl>
1 a     NA    c     NA   
2 NA    NA    c     NA   
3 NA    NA    c     NA   
4 a     b     c     NA   
5 NA    b     c     NA 
  • Related