我在一列中有一组单词,我想检查其中是否存在 "o "或 "u"。如果这个词有 "o "或 "u",我想用 "1 "新建一列,如果没有,我想用 "0"。
ID Word
1 rabbit
2 horse
3 tunnel
4 table
数据现在的样子的例子。
ID Word Subset_Vowel
1 rabbit 0
2 horse. 1
3 tunnel 1
4 table 0
这就是我想要的数据的样子。
我试着写了这段代码: DryRun_data_df$Subset_Vowel <- ifelse(str_detect(words, "o" 存在 "o "和 "u")
提前感谢大家的帮助!!!!。
与 ||
然后用 "二进制 "来胁迫逻辑。|
(或 as.integer
)+
或在
library(stringr)
df1$Subset_Vowel <- as.integer(str_detect(df1$Word, "o|u"))
df1
# ID Word Subset_Vowel
#1 1 rabbit 0
#2 2 horse 1
#3 3 tunnel 1
#4 4 table 0
与 base R
资料grepl
df1$Subset_Vowel <- +(grepl("o|u", df1$Word))
df1 <- structure(list(ID = 1:4, Word = c("rabbit", "horse", "tunnel",
"table")), class = "data.frame", row.names = c(NA, -4L))