dataframe - average number of words in a character vector in R -
i'm trying average number of words in character vector in r
one <- c(9, 23, 43) 2 <- c("this new york times article.", "short article.", "he went outside smoke cigarette.") mydf <- data.frame(one, two) mydf # 1 2 # 1 9 new york times article. # 2 23 short article. # 3 43 went outside smoke cigarette. i'm looking function gives me average number of words of character vector "two".
the output here should 5.3333 (=(7+2+7)/3)
or gregexpr()
mean(sapply(mydf$two,function(x)length(unlist(gregexpr(" ",x)))+1)) [1] 5.333333
Comments
Post a Comment