我有这个示例数据框
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
我想更新最后一栏“情况”。特别是,
所以我想拥有
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) ))
我不知道错误在哪里。我的真实数据集包含数千行。
看起来你可以用
gsub
来简化
library(dplyr)
df %>%
mutate(Situation = if_else(global_M == "Yes" & !is.na(audit_year),
gsub("(\\d+)", "xa\\1", Situation),
Situation))
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