繁体   English   中英

R:计算包含特殊字符的术语数量(例如,数据集中的 [url]

[英]R: Count the number of terms that include special characters (e.g. [url] in a dataset

我有一个数据集,其中我已将超链接转换为 [url] - 请参阅底部的帖子示例。 我只是想通过使用 R 来计算“[url]”的频率。

我尝试了以下方法但没有成功:

data = read.csv(X: ....... ,tweets.csv)
word_split= strsplit(USER_POST, " ")
sum(stringr::str_count(USER_POST, "[url]"))

我也试过这个

sum(stringr::str_count(USER_POST, "\\b[url]\\b"))

结果为 0。但是,当我签入 Excel 时,它出现了大约 7 次。 谁能指导我做错了什么? 先感谢您。

在下面编辑更多细节:

USER_ID    USER_POSTS 
123        I like butterflies. 
234        I have found some information in this webpage [url] 
456        Find more information here [url] 

如果我正确理解您的问题,这应该是一个可行的解决方案:

library(stringr)
str_count(x, "\\[url\\]")
[1] 2

这里的关键是要考虑到[]字符是正则表达式中的元字符。 如果您想将它们作为文字字符进行匹配,则需要在 R 中使用双斜杠\\对它们进行转义。

或者, str_count允许您将元字符设置为fixed文字字符:

str_count(x, fixed("[url]"))
[1] 2

数据:

x <- "USER_ID USER_POSTS 123 I like butterflies. 234 I have found some information in this webpage [url] 456 Find more information here [url]"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM