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.
source to share
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
source to share