Below are two simple data frames. I would like to recode (collapse) the columns Sat1and Sat2so that all degrees of satisfaction are encoded just like Satisfied, and all degrees of dissatisfaction are encoded like Dissatisfied. Neutral will remain neutral. Consequently, these factors will have three levels - Satisfied, Dissatisfied, and Neutral.
I usually do this by linking data frames and using lapplyalong with the repeated code from the package car, for example:
DF1[2:3] <- lapply(DF1[2:3], recode, c('"Somewhat Satisfied"= "Satisfied","Satisfied"="Satisfied","Extremely Dissatisfied"="Dissatisfied"........etc, etc
I would like to accomplish this using the map functions, in particular at_map(to save the data frame, but I'm new to purrr, so feel free to offer other versions of the map) from purrr, as well as dplyr, tidyr ,stringr andggplot2`, so everything can be easily pipelined.
The example below is what I would like to execute, but for re-encoding, but I could not get it to work.
http://www.r-bloggers.com/using-purrr-with-dplyr/
I would like to use at_map or a similar map function to keep the original columns Sat1and Sat2, therefore, the encoded columns will be added to the data frame and renamed. It would be great if this step could also be included in the function.
, , purrr, .
Names<-c("James","Chris","Jessica","Tomoki","Anna","Gerald")
Sat1<-c("Satisfied","Very Satisfied","Dissatisfied","Somewhat Satisfied","Dissatisfied","Neutral")
Sat2<-c("Very Dissatisfied","Somewhat Satisfied","Neutral","Neutral","Satisfied","Satisfied")
Program<-c("A","B","A","C","B","D")
Pets<-c("Snake","Dog","Dog","Dog","Cat","None")
DF1<-data.frame(Names,Sat1,Sat2,Program,Pets)
Names<-c("Tim","John","Amy","Alberto","Desrahi","Francesca")
Sat1<-c("Extremely Satisfied","Satisfied","Satisfed","Somewhat Dissatisfied","Dissatisfied","Satisfied")
Sat2<-c("Dissatisfied","Somewhat Dissatisfied","Neutral","Extremely Dissatisfied","Somewhat Satisfied","Somewhat Dissatisfied")
Program<-c("A","B","A","C","B","D")
DF2<-data.frame(Names,Sat1,Sat2,Program)