Regex 如何计算文本(字符串)中的字数?
我有这个字符串向量(例如): 字符串的最后两个位置只有一个元素(即Regex 如何计算文本(字符串)中的字数?,regex,r,apply,sapply,Regex,R,Apply,Sapply,我有这个字符串向量(例如): 字符串的最后两个位置只有一个元素(即“tusla”和“laq”) 所以它应该回来 6 2 1 1 我该如何避开这个问题 你可以试试 sapply(gregexpr("\\S+", x), length) ## [1] 6 2 1 1 或者按照评论中的建议,您可以尝试 sapply(strsplit(x, "\\s+"), length) ## [1] 6 2 1 1 使用stringi包和stri\u计数: require(stringi) str <-
“tusla”
和“laq”
)
所以它应该回来
6 2 1 1
我该如何避开这个问题 你可以试试
sapply(gregexpr("\\S+", x), length)
## [1] 6 2 1 1
或者按照评论中的建议,您可以尝试
sapply(strsplit(x, "\\s+"), length)
## [1] 6 2 1 1
使用
stringi
包和stri\u计数
:
require(stringi)
str <- c(
"this is a string current trey",
"nospaces",
"multiple spaces",
" leadingspaces",
"trailingspaces ",
" leading and trailing ",
"just one space each")
> stri_count(str,regex="\\S+")
[1] 6 1 2 1 1 3 4
require(stringi)
str stri_计数(str,regex=“\\S+”)
[1] 6 1 2 1 1 3 4
使用qdap软件包中的wc功能
str <- c("this is a string current trey",
"feather rtttt",
"tusla",
"laq")
library("qdap")
wc(str)
sapply(strsplit(str,”),length)
适合我。@RomanLuštrik如果字符串在几个单词之间有多个空格怎么办?例如str@user3664020,答案在你链接的问题中<代码>STR1使用<代码>“s+”/“代码>而不是<代码> >“<代码> >代码> > Str拆S/<代码>将允许您考虑和忽略单词之间的多个空格。第二个答案在“前导和尾随”上失败,返回。第一个答案以空字符串失败。“返回1”。
sapply(strsplit(x, "\\s+"), length)
## [1] 6 2 1 1
require(stringi)
str <- c(
"this is a string current trey",
"nospaces",
"multiple spaces",
" leadingspaces",
"trailingspaces ",
" leading and trailing ",
"just one space each")
> stri_count(str,regex="\\S+")
[1] 6 1 2 1 1 3 4
str <- c("this is a string current trey",
"feather rtttt",
"tusla",
"laq")
library("qdap")
wc(str)
wc(str)
[1] 6 2 1 1