根据条件创建新列

Create a new column from conditions

我有一个包含一些国家和州信息的数据框,如下所示:

data.frame("state1"= c(NA,NA,"Beijing","Beijing","Schleswig-Holstein","Moskva",NA,"Moskva",NA,"Berlin"), 
               "country1"=c("Spain","Spain","China","China","Germany","Russia","Germany","Russia","Germany","Germany"),
"state2"= c(NA,NA,"Beijing",NA,NA,NA,"Moskva",NA,NA,NA), 
"country2"=c("Germany","Germany","China","Germany","","Ukraine","Russia","Germany","Ukraine",""  ),
"state3"= c(NA,NA,NA,NA,"Schleswig-Holstein",NA,NA,NA,NA,"Berlin"), 
               "country3"=c("Spain","Spain","Germany","Germany","Germany","Germany","Germany","Germany","Germany","Germany"))

现在,我想创建一个包含德国各州信息的新栏目。 (结果如下所示)。 当三个变量state中至少有一个是德国国家时,将其赋值到新变量中。

data.frame("GE_State"=c(NA,NA,NA,NA, "Schleswig-Holstein",NA,NA,NA,NA,"Berlin"))

请帮助初学者设置条件。 提前致谢!

我想你想要 cbind() 这里:

df1 <- cbind(df1, df2)

数据:

df1 <- <your first data frame>
df2 <- data.frame("GE_State"=c(NA,NA,NA,NA, "Schleswig-Holstein",NA,NA,NA,NA,"Berlin"))

dplyr::mutate()case_when() 一起使用是可行的,尽管我怀疑使用 across()

应该有更有效的方法

library(dplyr)

  df %>% 
  mutate(GE_state = case_when(country1 == "Germany" & !is.na(state1) ~ state1,
                              country2 == "Germany" & !is.na(state2) ~ state2,
                              country3 == "Germany" & !is.na(state3) ~ state3,
                              TRUE ~ NA_character_))


#>                state1 country1  state2 country2             state3 country3
#> 1                <NA>    Spain    <NA>  Germany               <NA>    Spain
#> 2                <NA>    Spain    <NA>  Germany               <NA>    Spain
#> 3             Beijing    China Beijing    China               <NA>  Germany
#> 4             Beijing    China    <NA>  Germany               <NA>  Germany
#> 5  Schleswig-Holstein  Germany    <NA>          Schleswig-Holstein  Germany
#> 6              Moskva   Russia    <NA>  Ukraine               <NA>  Germany
#> 7                <NA>  Germany  Moskva   Russia               <NA>  Germany
#> 8              Moskva   Russia    <NA>  Germany               <NA>  Germany
#> 9                <NA>  Germany    <NA>  Ukraine               <NA>  Germany
#> 10             Berlin  Germany    <NA>                      Berlin  Germany
#>              GE_state
#> 1                <NA>
#> 2                <NA>
#> 3                <NA>
#> 4                <NA>
#> 5  Schleswig-Holstein
#> 6                <NA>
#> 7                <NA>
#> 8                <NA>
#> 9                <NA>
#> 10             Berlin

reprex package (v1.0.0)

创建于 2021-03-31