Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用str_count计算整个单词/数字的出现次数_R_Regex_Stringr - Fatal编程技术网

在R中使用str_count计算整个单词/数字的出现次数

在R中使用str_count计算整个单词/数字的出现次数,r,regex,stringr,R,Regex,Stringr,与案例类似,我想使用stringr包的stru count来计算出现在句子向量中的多个单词和数字的出现次数 但我注意到,不仅计算整数,还计算部分数。例如: df <- c("honda civic 1988 with new lights","toyota auris 4x4 140000 km","nissan skyline 2.0 159000 km") keywords <- c("honda","civic","toyota","auris","nissan","skyli

与案例类似,我想使用stringr包的stru count来计算出现在句子向量中的多个单词和数字的出现次数

但我注意到,不仅计算整数,还计算部分数。例如:

df <- c("honda civic 1988 with new lights","toyota auris 4x4 140000 km","nissan skyline 2.0 159000 km")
keywords <- c("honda","civic","toyota","auris","nissan","skyline","1988","1400","159")
library(stringr)
number_of_keywords_df <- str_count(df, paste(keywords, collapse='|'))

df尝试在关键词周围设置单词边界:

keywords <- c("honda","civic","toyota","auris","nissan","skyline","1988","1400","159")
keywords <- paste0("\\b", keywords, "\\b")

关键字使用sprintf可以添加单词边界:

number_of_keywords_df <- str_count(df, paste(sprintf("\\b%s\\b", keywords), collapse = '|'))
number_of_keywords_df
[1] 3 2 2