在mutate中使用带有case_when的str_subset吗?

时间:2020-02-24 02:46:56

标签: string dplyr mutate

在mutate中使用case_w来指定特定条件时遇到了一些麻烦。我正在尝试创建一个名为treatment的新列,其中,如果一个国家(在列名中)的名称以元音开头,则treatment列的读数为“ 1”。如果国家/地区的名称不是以元音开头,则我希望处理列显示为“ 0”。我在这里做了一些尝试,但似乎没有任何效果。

mutate("treatment" = 
        case_when
        (str_subset(name, pattern = "^[AEIOU]")) ~"1", 
         str_subset(name, pattern = "[^AEIOU]") ~ "0")

当前错误消息显示为:错误:列treatment的引用调用不受支持。

如果有人可以提供帮助,我将非常感谢!

1 个答案:

答案 0 :(得分:1)

我创建了一个小例子,希望对您有所帮助。

要考虑的一些事情:

  1. case_when()中参数的左侧必须是逻辑语句(即TRUEFALSE结果)。您使用的str_subset()函数返回的字符串符合您的条件,而不是逻辑字符串。在下面的示例中,我使用str_starts()来返回与您输入条件匹配的逻辑。

  2. case_when()中会忽略
  3. NULL值,但是如果愿意,您也可以指定如何处理它们。有关示例,请查看文档?case_when

祝你好运,欢迎来到R!

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(stringr)

# create data frame with countries, include NA for demonstration
df <- tibble(
 country = c("Columbia", "Uruguay", "Argentina", "Brazil", NA)
)

df2 <- 
  df %>% 
  mutate(
    starts_vowel = 
      case_when(
      # left hand side of case_when must be a logical
      str_starts(country, "A|E|I|O|U") ~ 1,
      #Adding negate = TRUE returns non-matching
      str_starts(country, "A|E|I|O|U", negate = TRUE) ~ 0, 
      )
  )

df2
#> # A tibble: 5 x 2
#>   country   starts_vowel
#>   <chr>            <dbl>
#> 1 Columbia             0
#> 2 Uruguay              1
#> 3 Argentina            1
#> 4 Brazil               0
#> 5 <NA>                NA

# Check out the difference between str_subset and #str_starts
str_subset(df$country, "^[A|E|I|O|U]")
#> [1] "Uruguay"   "Argentina"
str_starts(df$country, "A|E|I|O|U")
#> [1] FALSE  TRUE  TRUE FALSE    NA

reprex package(v0.3.0)于2020-02-24创建

相关问题