내가 나에게 평균 수를 제공하는 기능을 찾고 있어요평균
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.
R 내 문자 벡터 단어의 평균 개수를 얻으려고 문자 벡터 "2"단어의
library(stringr)
foo<- str_split(two, " ") # split each element of your vector by the space sign
sapply(foo,length) # just a quick test: how many words has each element?
sum(sapply(foo,length))/length(foo) # calculate sum and divide it by the length of your original object
[1] 5.333333
'(sapply (gregexpr ("", mydf $ 2), length) +1)'같은 개념이지만 좀 더 간결합니다 .... – A5C1D2H2I1M1N2O1R2T1
@AnandaMahto 예 좋은 지적, 그 일을 먼저하지 말아라. – Troy
내 제안 *은 gregexpr에 대한 호출 횟수를 줄이기 때문에 내 제안을 사용했다면 속도 향상을 얻을 수 있다고 생각한다. 나는 또한 실제 해결책은 (1) 거기에있을 수있는 앞뒤의 공백을 먼저 제거하고 (2) 검색 용어를 "\\ s +"'와 같이 만들 것을 제안합니다. – A5C1D2H2I1M1N2O1R2T1