Producing two new variables using recode in R?

Viewed 38

I'm gradually getting used to recoding variables in R but I'm having a bit of trouble creating two new variables. For example, I have tried the following:

income2018$income2 <- dplyr::recode(income2018$income, '51' = 1L, '52' = 1L, '53' = 2L)
income2018$income3 <- dplyr::recode(income2018$income, '57' = 1L, '58' = 1L, '50' = 2L)

It doesn't look like the values are being correctly applied to the new variables.

Here is the SPSS syntax that I am attempting to recreate:

RECODE income (51,52=1)(53=2) into income2
RECODE income (57,58=1)(50=2) into income3

I'd be very grateful for any assistance.

Many thanks.

1 Answers

It looks like you might need to rearrange your code a little bit, but it's hard to tell without a reprex

You might want to try:

income2018 <- income2018 %>%
dplyr::mutate(income2 = income) %>%
dplyr::recode(income2, '51' = 1L, '52' = 1L, '53' = 2L)
Related