1

I have a dataframe with different variables containing values from 1 to 5. I want to recode some variables in the way that 5 becomes 1 and vice versa (x=6-x). I want to define a list of variables, that will be recoded like this in my dataframe.

Here is my approach using lapply. I haven't really understood it yet.

  #generate example-dataset
    var1<-sample(1:5,100,rep=TRUE)
    var2<-sample(1:5,100,rep=TRUE)
    var3<-sample(1:5,100,rep=TRUE)
    dat<-as.data.frame(cbind(var1,var2,var3))

    recode.list<-c("var1","var3")  
    recode.function<- function(x){          
    x=6-x
     }
    lapply(recode.list,recode.function,data=dat)
1
  • 1
    calculating 6-x in this case is pretty simple but if you have more specific recoding patterns you might want to check the custom "recode" function from the "car" package. Commented Nov 20, 2014 at 13:52

3 Answers 3

6

There's no need for an external function or for a package for this. Just use an anonymous function in lapply, like this:

df[recode.list] <- lapply(df[recode.list], function(x) 6-x)

Using [] lets us replace just those columns directly in the original dataset. This is needed since just using lapply would result in the data as a named list.


As noted in the comments, you can actually even skip lapply:

df[recode.list] <- 6 - df[recode.list] 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. Somehow, i get following message when applying your code to my data: "Error in [.data.frame(df, recode.list) : undefined columns selected". What could this possibly mean?
@user2982730, sounds like it means that your "recode.list" includes columns that are not found in the source data.frame.
1

You can use mapvalues from plyr.

require(plyr)
# if you just want to replace 5 with 1 and vice versa
df[, recode.list] <- sapply(df[, recode.list], mapvalues, c(1, 5), c(5,1))
# if you want to apply to x=6-x to all values (in this case you don't need mapvalues)
df[, recode.list] <- sapply(df[, recode.list], mapvalues, 1:5, 5:1)

1 Comment

mapvalues didn't work for me. instead I used recode from the car package. That said, recode is only useful when one knows how many different values the variable has and there aren't too many values that need to be recoded.
1

Here's an option to do this with dplyr:

recode.function<- function(x){          
  x <- 6-x 
}

recode.list <- c("var1","var3") 

require(dplyr)
df %>% mutate_each_(funs(recode.function), recode.list)

#    var1 var2 var3
#1      2    2    4
#2      3    3    3
#3      3    5    2
#4      3    3    2
#5      4    3    3
#6      5    4    1
#...

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.