在R中,查找一个函数来计算另一行值中一行值(完整字符串)的出现次数 例如:数据表df
df$SerachingFor df$Target df$SearchTermOccurrencesInTarget (required field)
Machine rowing machine rowing has two values 1
handy tools handy tools, we have many handy tools 2
plans home plans, garden plans, DIY plans 3
hospital home ideas 0
欣赏你的时间
答案 0 :(得分:2)
以下是stringr
库函数的一种方法。请注意,我将两列都转换为所有小写字母,因为您的示例意味着不区分大小写匹配。
library(stringr)
df$SerachingFor <- tolower(df$SerachingFor)
df$Target <- tolower(df$Target)
df$SearchTermOccurrencesInTarget <- str_count(df$Target, df$SerachingFor)
df
SerachingFor Target SearchTermOccurrencesInTarget
1 machine rowing machine rowing has two values 1
2 handy tools handy tools we have many handy tools 2
3 plans home plans garden plans DIY plans 3
4 hospital home ideas 0