我有这个示例数据框
df<-data.frame(old_farm=c("Yes", "Yes","Yes", "No", "No", "No" , NA ),
env_year=c(2011, 2020,2019,2010,2010,2010, NA),
global_M=c("Yes", "Yes", "No", "Yes","Yes", "No", NA ),
audit_year=c(2014, NA,NA,2010,NA,NA,NA),
Situation=c("New costat 2011, Closed antex 2014",
"New costat 2020",
"New costat 2019",
"Closed antex 2010",
"missing entry",
"Full exit", "Not at all"))
df
old_farm env_year global_M audit_year Situation
1 Yes 2011 Yes 2014 New costat 2011, Closed antex 2014
2 Yes 2020 Yes NA New costat 2020
3 Yes 2019 No NA New costat 2019
4 No 2010 Yes 2010 Closed antex 2010
5 No 2010 Yes NA missing entry
6 No 2010 No NA Full exit
7 <NA> NA <NA> NA Not at all
我想更新最后一列“情况”。特别是,
- 如果 old_farm ==“是”&global_M ==“是”&!is.na(audit_year),那么我希望在情况列中包含:“新 costat xa”、env_year、“关闭 antex xa”、audit_year;其中 env_year 和 audit_year 是相应列中的年份。
- 如果 old_farm ==“否”&global_M ==“是”&!is.na(audit_year),那么我希望在最后一列“Closed antex xa”中包含audit_year,其中audit_year是相应列的年份。
所以我想
df_n<-data.frame(old_farm=c("Yes", "Yes","Yes", "No", "No", "No" , NA ),
env_year=c(2011, 2020,2019,2010,2010,2010, NA),
global_M=c("Yes", "Yes", "No", "Yes","Yes", "No", NA ),
audit_year=c(2014, NA,NA,2010,NA,NA,NA),
Situation=c("New costat xa2011, Closed antex xa2014",
"New costat 2020",
"New costat 2019",
"Closed antex xa2010",
"missing entry",
"Full exit", "Not at all"))
df_n
old_farm env_year global_M audit_year Situation
1 Yes 2011 Yes 2014 New costat xa2011, Closed antex xa2014
2 Yes 2020 Yes NA New costat 2020
3 Yes 2019 No NA New costat 2019
4 No 2010 Yes 2010 Closed antex xa2010
5 No 2010 Yes NA missing entry
6 No 2010 No NA Full exit
7 <NA> NA <NA> NA Not at all
因此我执行了这三个替代命令,但是它们都不起作用
library(dplyr)
df %>%
mutate(
Situation = case_when(
old_farm == "Yes" & global_M == "Yes" & !is.na(audit_year) ~
paste0("New costat xa", env_year, ", Closed antex xa", audit_year),
old_farm == "No" & global_M == "Yes" & !is.na(audit_year) ~
paste0("Closed antex xa", audit_year) ))
df %>%
mutate(across("Situation") , case_when(
old_farm == "Yes" & global_M == "Yes" & !is.na(audit_year) ~
paste0("New costat xa", env_year, ", Closed antex xa", audit_year) ,
old_farm == "No" & global_M == "Yes" & !is.na(audit_year) ~
paste0("Closed antex xa", audit_year) ))
df %>%
mutate_at("Situation",~ case_when(
old_farm == "Yes" & global_M == "Yes" & !is.na(audit_year) ~
paste0("New costat xa", env_year, ", Closed antex xa", audit_year),
old_farm == "No" & global_M == "Yes" & !is.na(audit_year) ~
paste0("Closed antex xa", audit_year) ))
我不确定错误在哪里。我的真实数据集包含数千行。
没有改进的余地,你的方法没有问题。也许你只是忘了使用
.default