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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -