我有如下数据:
head(orthographic)
Word Root
1 manful man
2 carful care
3 basketful basket
4 meaningful mean
5 boxful box
6 armsful <NA>
而且我想看看Root
字是否在Word
之内。例如,man
包含在manful
中,并且拼写不变。但是,car
变为careful
。
My ideal output
Word Root C
1 manful man No
2 carful care Yes
3 basketful basket No
4 meaningful mean No
5 boxful box No
6 armsful <NA> <NA>
我尝试过的(基于其他人的帖子):
a=orthographic$Word
b=orthographic$Root
df$a %in% df$b
charmatch(df$a,df$b)
grepl(df$a,df$b)
grep(df$a,df$b)
但是,它们都不给我我想要的输出。特别是,所有产生的代码:
2 carful care "NO" # (Instead of "Yes").
我感谢任何输入/解决方案。
str_detect
中的stringr
df$C <- c("Yes", "No")[stringr::str_detect(df$Word, df$Root) + 1]
df$C[is.na(df$Root)] <- NA
df
# Word Root C
#1 manful man No
#2 carful care Yes
#3 basketful basket No
#4 meaningful mean No
#5 boxful box No
#6 armsful <NA> <NA>
ifelse
语句(使用str_detect
的想法,致谢@Ronak Shah)