如何使用 `case_when` 根据字符串检测和 R 中的当前列值有条件地更改值?

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

我正在尝试根据未检测到的字符串和列中的当前值有条件地更改列中的值。

starwars %>%
  mutate(
    species = case_when(
      !str_detect(name, "Luke Skywalker") |
        !str_detect(name, "LUKE SKYWALKER") |
        !str_detect(name, "Darth Vader") |
        !str_detect(name, "DARTH VADER") &
        species == "Human"
      ~ "Other",
      .default = species
    )
  )

结果是物种的所有值都变成“其他”。我本以为只有人类角色(不是卢克·天行者或达斯·维达)是“其他物种。

r tidyverse case
© www.soinside.com 2019 - 2024. All rights reserved.