多亏了 dplyr,现在这相当简单了recode
功能。这是一种方法:
# Generate a dataframe to match yours
df <- data.frame(
participant = c(1,2),
Q1 = c("agree", "neutral"),
Q2 = c("neutral", "agree"),
Q3 = c(NA,NA),
Q4 = c("Disagree", NA),
Q5 = c("Agree", NA)
)
# Use recode to recode the data
df_recode <- df %>%
mutate(Q1 = recode(Q1, "agree" = 3, "neutral" = 2),
Q2 = recode(Q2, "neutral" = 2, "agree" = 1),
Q4 = recode(Q4, "Disagree" = 1),
Q5 = recode(Q5, "Agree" = 3)
)
您还想阅读有关.default
and .missing
帮助文件中的参数以确保您没有引入NAs
当你无意的时候。