Search code examples
rreplacedplyrrecode

Recode multiple columns in dataframe R


I am trying to convert a Y into a 1 for a number of columns that could change (e.g. it could go up to x20).

An example of the data is below as well as an expected output.

Data <- tibble(Date = seq.Date(as.Date('2019-01-01'),as.Date('2019-01-08'), by = "day"), 
               x1 = c("Y","","","Y","Y","","Y","Y"),
               x2 = c("","Y","Y","Y","Y","","Y","Y"))


Data_output <- tibble(Date = seq.Date(as.Date('2019-01-01'),as.Date('2019-01-08'), by = "day"), 
               x1 = c(1,0,0,1,1,0,1,1),
               x2 = c(0,1,1,1,1,0,1,1))

Solution

  • With dplyr:

    Data %>% 
      mutate_at(vars(contains("x")),~case_when(.=="Y" ~1,
                                               .=="" ~0))
    

    or as suggested by @akrun simply:

    Data %>% 
      mutate_at(vars(contains("x")), ~as.integer(.=="Y"))  
    

    Result:

    # A tibble: 8 x 3
      Date          x1    x2
      <date>     <dbl> <dbl>
    1 2019-01-01     1     0
    2 2019-01-02     0     1
    3 2019-01-03     0     1
    4 2019-01-04     1     1
    5 2019-01-05     1     1
    6 2019-01-06     0     0
    7 2019-01-07     1     1
    8 2019-01-08     1     1