R: How to Recode Multiple Variables at Once

R: How to recode multiple variables at once

This is neater I think with dplyr. Using recode correctly is a good idea. mutate_all() can be used to operate on the whole dataframe, mutate_at() on just selected variables. There are lots of ways to specify variables in dplyr.

mydata <- data.frame(arg1=c(1,2,4,5),arg2=c(1,1,2,0),arg3=c(1,1,1,1))

mydata

arg1 arg2 arg3
1 1 1 1
2 2 1 1
3 4 2 1
4 5 0 1

mydata <- mydata %>%
mutate_at(c("arg1","arg2"), funs(recode(., `1`=-1, `2`=1, .default = NaN)))

mydata

arg1 arg2 arg3
1 -1 -1 1
2 1 -1 1
3 NaN 1 1
4 NaN NaN 1

I use NaN instead of NA as it is numeric is be simpler to manage within a column of other numbers.

What is the shortest and cleanest way to recode multiple variables in a dataframe using R?

I think if used correctly, dplyr has the "cleanest" syntax in this case:

library(dplyr)
tib <- tibble(v1 = 1:4,
v2 = 1:4,
v3 = sample(1:5, 4, replace = FALSE))

tib %>%
mutate_at(vars(v1:v3), recode, `1` = 5, `2` = 4, `3` = 3, `4` = 2, `5` = 1)
#> # A tibble: 4 x 3
#> v1 v2 v3
#> <dbl> <dbl> <dbl>
#> 1 5 5 2
#> 2 4 4 5
#> 3 3 3 4
#> 4 2 2 1

Note that I had to add 3 = 3 because recode needs a replacement for all values.

I often find it easier to write things more explicitly with functions that are new to me, so maybe this might help:

tib %>% 
mutate_at(.vars = vars(v1:v3),
.funs = function(x) recode(x,
`1` = 5,
`2` = 4,
`3` = 3,
`4` = 2,
`5` = 1))

If you prefer the recode function from car you should not load car but use:

tib %>% 
mutate_at(vars(v1:v3), car::recode, "1=5; 2=4; 4=2; 5=1")

That way you don't run into trouble mixing dplyr with car (as long as you don't need car for anything else.

Recoding multiple variables on different scales using across()

One option could be:

data %>%
mutate(across(Var1:Var2, ~ car::recode(., get(paste0(cur_column(), "_recode")))))

ID Var1 Var2
1 1 yes unknown
2 2 no moderate
3 3 yes weak
4 4 no strong
5 5 no moderate

How do I recode multiple variables from string to numeric?

The following method seems to have worked for my issue (recoding string variables to numeric in multiple columns):

For_Analysis <- data.frame(Q11_1=c("Never", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))

New_Values <- c(1, 2, 3, 4, 5)
Old_Values <- unique(For_Analysis$Q11_1)

For_Analysis[1:3] <- as.data.frame(sapply(For_Analysis[1:3],
mapvalues, from = Old_Values, to = New_Values))

Thanks for the help!

How can I recode multiple variables with the same mapping at once in R?

We can use mutate with across, specify the column names to be recoded in across to modify those columns

mhomes <- mhomes %>%
mutate(across(c(contrib_private_3rd_party, contrib_firm_3rd_party),
~ recode(., `1` = 1,
`2` = 49,
`3` = 100,
`4` = 200,
`5` = 500,
`6` = 1000,
`7` = 5000,
`8` = 10000,
`9` = 20000)))

R recode multiple variables following same rules

We need to use replace with lapply

data[recode] <- lapply(data[recode], function(x) replace(x, x %in% 4:5, NA))
data
# x1 x2 x3 x4 x5
#1 1 1 1 21 35
#2 2 2 2 22 36
#3 3 3 3 23 37
#4 NA NA 2 24 38
#5 NA NA 3 25 39
#6 6 6 NA 26 40
#7 7 7 NA 27 41
#8 8 8 NA 28 42
#9 9 9 6 29 43
#10 10 10 7 30 44

Or with dplyr

library(dplyr)
data %>%
mutate_at(vars(recode), ~ na_if(., 4)) %>%
mutate_at(vars(recode), ~ na_if(., 5))
# x1 x2 x3 x4 x5
#1 1 1 1 21 35
#2 2 2 2 22 36
#3 3 3 3 23 37
#4 NA NA 2 24 38
#5 NA NA 3 25 39
#6 6 6 NA 26 40
#7 7 7 NA 27 41
#8 8 8 NA 28 42
#9 9 9 6 29 43
#10 10 10 7 30 44


Related Topics



Leave a reply



Submit