Recode multiple variables in R

I would like to recode several variables at once into R. The variables are within a larger data frame. Here are some sample data:

 z <- data.frame (A = c(1,2,300,444,555),
              B = c(555,444,300,2,1),
              C = c(1,2,300,444,555),
              D = c(1,2,300,444,555))

      

What I would like to do is recode all values ​​that are 300 as 3, 444 as 4 and 555 as 5.

I thought I could do it on the list. Here's what I've tried:

example_list  = list(c("A", "B", "C", "D"))

example_list <- apply(z[,example_list], 1, function(x) ifelse(any(x==555, na.rm=F), 0.5,
                                                              ifelse(any(x==444), 0.25),
                                                              ifelse(any(x==300), 3, example_list)))

      

I am getting this error:

Error during wrapup: invalid subscript type 'list'

      

Then tried to use "lapply" and I got this error:

Error during wrapup: '1' is not a function, character or symbol

      

Even then, I'm not sure if this is the best way to do it ... I would just like to avoid it in turn for multiple variables. Any suggestions would be awesome as I'm new to R and don't quite understand what I'm doing wrong.

I found similar questions on SO: Question , but I'm not sure how to apply this to my specific problem.

+3


source to share


5 answers


It seems a little clunky, but it works:



mutate_cols <- c('A', 'B')

z[, mutate_cols] <- as.data.frame(lapply(z[, mutate_cols], function(x) ifelse(x == 300, 3, 
                                                                              ifelse(x == 444, 4, 
                                                                                     ifelse(x== 555, 5, x)))))

      

+1


source


Usage case_when

:



library(dplyr)
z %>% mutate_all(
    function(x) case_when(
        x == 300 ~ 3,
        x == 444 ~ 4,
        x == 555 ~ 5,
        TRUE ~ x
    )
)

  A B C D
1 1 5 1 1
2 2 4 2 2
3 3 3 3 3
4 4 2 4 4
5 5 1 5 5

      

+1


source


Here's a basic attempt at R, which should be neatly extensible and pretty fast:

# set find and replace vectors
f <- c(300,444,555)
r <- c(3, 4, 5)
# replace!
m   <- lapply(z, function(x) r[match(x,f)] )
z[] <- Map(function(z,m) replace(m,is.na(m),z[is.na(m)]), z, m)

#  A B C D
#1 1 5 1 1
#2 2 4 2 2
#3 3 3 3 3
#4 4 2 4 4
#5 5 1 5 5

      

+1


source


z =  data.frame (A = c(1,2,300,444,555),
                 B = c(555,444,300,2,1),
                 C = c(1,2,300,444,555),
                 D = c(1,2,300,444,555))



library(expss)

to_recode = c("A", "B", "C", "D")
recode(z[, to_recode]) = c(300 ~ 3, 444 ~ 4, 555 ~ 5)

      

0


source


This should work.

library(plyr) 
new.z<- apply(z, 1, function(x) mapvalues(x, from = c(300, 444, 555), to = c(3, 4, 5)))

      

0


source







All Articles