【发布时间】:2023-02-05 11:32:29
【问题描述】:
我的 df 包含 50 个人的人口统计信息。我的 df 中有一个名为“种族”的专栏,其中包含许多种族类别,包括“英国白人”、“其他白人”和“爱尔兰白人”。我想创建一个新列,其中所有具有这 3 个值之一的观察值都被归类为“白色”,所有不以“白色”开头的观察值都被归类为“POC”。
df %>% mutate(Status = case_when(startsWith(Ethnicity, "White") ~ "White"))
我收到以下错误
Error in `mutate()`:
! Problem while computing `Status = case_when(startsWith(Ethnicity,
"White") ~ "White")`.
Caused by error in `startsWith()`:
! non-character object(s)
Run `rlang::last_error()` to see where the error occurred.
【问题讨论】:
-
这不是字符列应该出现的错误,
df$Ethnicity是一个因素吗?您可以通过str(df$Ethnicity)查看。或者更好的是,如果您可以共享数据集中的位,请将dput(head(df))的输出添加到您的问题中。
标签: r startswith mutate