【发布时间】:2023-04-08 06:28:01
【问题描述】:
假设的数据框:
strings new column
mesh 1
foo 0
bar 0
tack 1
suture 1
如果 df$strings 包含字符串“mesh”、“tack”或“sutur”,我希望新列包含“1”。否则它应该在同一行显示零。我尝试了以下方法:
df$new_column <- ifelse(grepl("mesh" | "tack" | "sutur",
df$strings, ignore.case = T), "1", "0")
但出现此错误:
Error in "mesh" | "tack" :
operations are possible only for numeric, logical or complex types
提前致谢。
【问题讨论】:
标签: regex r dataframe pattern-matching match