使用 mutate 和 case_when 更新数据帧的列

问题描述 投票:0回答:1

我有这个示例数据框

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

我想更新最后一栏“情况”。特别是,

  1. 如果 old_farm == "Yes" & global_M == "Yes" & !is.na(audit_year),那么我想在“情况”列中包含:“New costat xa”,env_year,“Closed antex xa”,audit_year;其中 env_year 和audit_year 是相应列中的年份。
  2. 如果 old_farm == "No" & global_M == "Yes" & !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)  ))

我不知道错误在哪里。我的真实数据集包含数千行。

r dataframe
1个回答
0
投票

看起来你可以用

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
© www.soinside.com 2019 - 2024. All rights reserved.