Counting the total number of words in rows of a data block

I have a data frame with words in each row. An example of some lines:

df
This is a word
Another word
third word
word

And I want to count the number of each row and write it to a new data frame and have something like this in the last csv:

df,total
This is a word,4
Another word,2
third word,2
word,1

Is it possible to use a space character?

+4
source share
2 answers

Just use strsplitwith the desired section, and then count the number of elements that come out.

df$total <- sapply(df$df, function(x) length(unlist(strsplit(as.character(x), "\\W+"))))

gives me the following result:

              df total
1 This is a word     4
2   Another word     2
3     third word     2
4           word     1
+7
source

you can use str_count

library(stringr)
df$total <- str_count(df$df, '\\s+')+1
df$total
#[1] 4 2 2 1

or

 nchar(gsub('[^ ]+', '',df$df))+1
 #[1] 4 2 2 1

or

 lengths(strsplit(df$df, '\\S+'))
 #[1] 4 2 2 1

or

 count.fields(textConnection(df$df))
 #[1] 4 2 2 1
+7
source

All Articles