1

I have a dataframe, where one column contains strings.

q = data.frame(number=1:2,text=c("The surcingle hung in ribands from my body.", "But a glance will show the fallacy of this idea."))

I want to use the word_stats function for each individual record. is it possible?

Akhil
  • 165
  • 1
  • 1
  • 8

3 Answers3

0
text_statistic <- apply(q,1,word_stats)

this will apply word_stats() row-by-row and return a list with the results of word_stats() for every row

f.lechleitner
  • 3,554
  • 1
  • 17
  • 35
0

you can do it many ways, lapply or sapply apply a Function over a List or Vector.

word_stats <- function(x) {length(unlist(strsplit(x, ' ')))} sapply(q$text, word_stats)

myincas
  • 1,500
  • 10
  • 15
0

Sure have a look at the grouping.var argument:

dat = data.frame(number=1:2,text=c("The surcingle hung in ribands from my body.", "But a glance will show the fallacy of this idea."))


with(dat, qdap::word_stats(text, number))

##   number n.sent n.words n.char n.syl n.poly wps cps sps psps   cpw   spw pspw n.state p.state n.hapax grow.rate
## 1 2           1      10     38    14      2  10  38  14    2 3.800 1.400 .200       1       1      10         1
## 2 1           1       8     35    12      1   8  35  12    1 4.375 1.500 .125       1       1       8         1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519