Ster32
Ster32

Reputation: 415

Counting the total number of words in of rows of a dataframe

I have a data frame with words in every row. Example of some rows:

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 in a final csv something like this:

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

Possible using the space character?

Upvotes: 9

Views: 9060

Answers (2)

akrun
akrun

Reputation: 887871

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

Upvotes: 11

chappers
chappers

Reputation: 2415

Simply use strsplit with the split you wish and then count the number of items which come out.

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

gives me the following output:

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

Upvotes: 10

Related Questions