我试图让字的平均数在我的特征向量中的R平均数的R中的字符矢量字的
one <- c(9, 23, 43)
two <- c("this is a new york times article.", "short article.", "he went outside to smoke a cigarette.")
mydf <- data.frame(one, two)
mydf
# one two
# 1 9 this is a new york times article.
# 2 23 short article.
# 3 43 he went outside to smoke a cigarette.
我要找的,让我平均数的函数字符向量“two”的词语。
这里的输出应该是5.3333(=(7 + 2 + 7)/ 3)
'平均(sapply(gregexpr(“”,mydf $ 2),长度+1)'是相同的概念,但更简洁一点.... – A5C1D2H2I1M1N2O1R2T1
@AnandaMahto是好点,不知道为什么我没有' t首先这样做 – Troy
我的猜测*是,如果您使用我的建议,您将获得速度提升,因为它可以减少对“gregexpr”的调用次数。我还建议实际的解决方案应该包括:(1)首先修剪任何可能存在的前后空格;(2)使搜索词类似'“\\ s +”'。 – A5C1D2H2I1M1N2O1R2T1