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


i have data frame words in every row. example of rows:

df word word third word word 

and want count number of each row , write new data frame , have in final csv this:

df,total word,4 word,2 third word,2 word,1 

possible using space character?

simply use strsplit split wish , count number of items come out.

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

gives me following output:

              df total 1 word     4 2   word     2 3     third word     2 4           word     1 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -