R 一次选择并重新编码多个变量
我有一个数据集,我想把大部分变量重新编码成一个数值变量,但我很难弄清楚如何使它工作。我需要以相同的方式重新编码大多数变量,但有两个变量需要反向编码。我的数据帧:R 一次选择并重新编码多个变量,r,dplyr,recode,data-wrangling,R,Dplyr,Recode,Data Wrangling,我有一个数据集,我想把大部分变量重新编码成一个数值变量,但我很难弄清楚如何使它工作。我需要以相同的方式重新编码大多数变量,但有两个变量需要反向编码。我的数据帧: Section B14 B24 B22 B13 B7 B17 F14 F24 F22 F7 F24 Kim Agree Agree Neutral Agree Disagree Agree Neutral Disagree A
Section B14 B24 B22 B13 B7 B17 F14 F24 F22 F7 F24
Kim Agree Agree Neutral Agree Disagree Agree Neutral Disagree Agree Disagree Neutral
Kim Agree Disagree Neutral Agree Disagree Neutral Neutral Agree Agree Disagree Neutral
Kat Agree Agree Agree Agree Disagree Agree Neutral Disagree Agree Disagree Neutral
Kim Agree Agree Neutral Agree Agree Agree Neutral Disagree Agree Agree Neutral
Kat Agree Agree Disagree Agree Disagree Agree Neutral Disagree Agree Disagree Neutral
我想得到的是:
Section B14 B24 B22 B13 B7 B17 F14 F24 F22 F7 F24
Kim 4 4 3 4 Disagree 4 3 2 4 Disagree 3
Kim 4 2 3 4 Disagree 3 3 4 4 Disagree 3
Kat 4 4 4 4 Disagree 4 3 2 4 Disagree 3
Kim 4 4 3 4 Agree 4 3 2 4 Agree 3
Kat 4 4 2 4 Disagree 4 3 2 4 Disagree 3
我尝试了以下代码:
DF<-DF%> mutate(across(-B7, -F7), recode("Strongly Agree"= 5, "Agree"= 4, "Neutral"=3,"Disagree"= 2, "Strongly Disagree"= 1))
DF突变(跨越(-B7,-F7),重新编码(“强烈同意”=5,“同意”=4,“中立”=3,“不同意”=2,“强烈不同意”=1))
-及-
DF变异(vars(-B7,-F7),重新编码(“强烈同意”=5,“同意”=4,“中立”=3,“不同意”=2,“强烈不同意”=1))
-及-
DF突变(跨越(B14:B13,B7:F22,F24),重新编码(“强烈同意”=5,“同意”=4,“中立”=3,“不同意”=2,“强烈不同意”=1))
但这些都没有奏效。非常感谢您的帮助。谢谢 DF%>%变异(跨越(-c(B7,F7),重新编码,`Strongly Agree`=5,Agree=4,Neutral=3,Disagree=2,`Strongly Disagree`=1))
DF<-DF%> mutate(vars(-B7, -F7), recode("Strongly Agree"= 5, "Agree"= 4, "Neutral"=3,"Disagree"= 2, "Strongly Disagree"= 1))
DF<-DF%> mutate(across(B14:B13, B7:F22, F24), recode("Strongly Agree"= 5, "Agree"= 4, "Neutral"=3,"Disagree"= 2, "Strongly Disagree"= 1))