Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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:统计数据集中包含特殊字符(例如[url])的术语数量_R_Stringr - Fatal编程技术网

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

R:统计数据集中包含特殊字符(例如[url])的术语数量,r,stringr,R,Stringr,我有一个数据集,在那里我把超链接转换成了[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_PO

我有一个数据集,在那里我把超链接转换成了[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\u count
允许您将元字符设置为
fixed
文字字符:

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


x您能提供一个数据片段,最好是reprex格式吗?否则很难解释各种细节。@Zoowwalk谢谢,我刚刚补充了更多内容information@RonakShah谢谢,我刚刚添加了更多的信息。嗨,克里斯,它工作得很好!!!非常感谢!!!我很高兴。请考虑一下。接受或提升答案。
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]"