我試圖讓字的平均數在我的特徵向量中的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