下面是两个简单的数据框。我想重新编码(折叠)Sat1
和Sat2
列,以便所有满意程度都简单编码为Satisfied
,所有不满意程度都编码为Dissatisfied
。中性将保持中性。因此,这些因素将具有三个级别 - Satisfied, Dissatisfied, and Neutral
。
我通常会通过绑定数据帧并与包lapply
中的重新代码一起使用来完成此操作car
,例如:
DF1[2:3] <- lapply(DF1[2:3], recode, c('"Somewhat Satisfied"= "Satisfied","Satisfied"="Satisfied","Extremely Dissatisfied"="Dissatisfied"........etc, etc
我想使用地图功能来完成此任务,特别是at_map
(维护数据框,但我是新手,purrr
因此可以随意建议其他版本的地图)来自purrr
tidyr stringr ggplot2` dplyr
,因此一切都可以轻松流水线化.,
and
下面的示例是我想要完成的,但要重新编码,但我无法使其工作。
http://www.r-bloggers.com/using-purrr-with-dplyr/
我想使用 at_map 或类似的 map 函数,这样我就可以保留 and 的原始列Sat1
,Sat2
因此重新编码的列将被添加到数据框中并重命名。如果这个步骤也可以包含在一个函数中,那就太好了。
实际上,我会有很多数据帧,所以我只想重新编码一次因子水平,然后使用函数 frompurrr
使用最少的代码对所有数据帧进行更改。
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)