2

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))

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks was trying to do this but was using funs.
1

In base R, we can do

Data[-1] <- +( Data[-1] == "Y")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.