Below are two simple data frames. I would like to re-code (collapse) the Sat1
and Sat2
columns so that all degrees of satisfied are coded simply as Satisfied
, and all degrees of Dissatisfied are coded as Dissatisfied
. Neutral will remain as Neutral. These factors will therefore have three levels - Satisfied, Dissatisfied, and Neutral
.
I would normally accomplish this by binding the data frames, and using lapply
along with re-code from the car
package, such as:
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 map functions, specifically at_map
(to maintain the data frame, but I'm new to purrr
so feel free to suggest other versions of map) from purrr
, as well as dplyr
, tidyr,
stringrand
ggplot2` so everything can be easily pipelined.
The example below is what I would like to accomplish, but for re-coding, but I was unable to make it work.
http://www.r-bloggers.com/using-purrr-with-dplyr/
I would like to use at_map or a similar map function so that I can keep the original columns of Sat1
and Sat2
, so the re-coded columns will be added to the data frame and renamed. It would be great if this step could also be included within a function.
In reality, I will have many data frames, so I only want to recode the factor levels once, and then use a function from purrr
to make the changes across all the data frames using the least amount of code.
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)