Search for the number of words in each line

Let's say that I want to find the number of words in each row of the data frame. So, in the following example, I want to find that the first value in column one has 3 words, the second value has 4 words, etc. I assume that this is a task for one of the functions used, but I know little about it.

dat = data.frame(one=c("That is Cool",
  "I like my bank", "He likes pizza", "What"))

Do I need to work with strsplit()or is it better to use a function apply()when creating a function:apply(dat, 1, function(x)...

+3
source share
2 answers

This code should do this, assuming all words are separated by spaces.

sapply(strsplit(as.character(dat$one), " "), length)
# [1] 3 4 3 1
+6
source

, . - , , (.. , +1 ):

nchar(gsub("[^ ]", "", dat$one)) + 1
# [1] 3 4 3 1

:

nchar(gsub("[^ ]|^ *| *$", "", dat$one)) + 1
# [1] 3 4 3 1

:

x <- c(" One two ", "One Two ", " One two")
nchar(gsub("[^ ]", "", x)) + 1
# [1] 4 3 3
sapply(strsplit(x, " "), length)
# [1] 3 2 3
nchar(gsub("[^ ]|^ *| *$", "", x)) + 1
# [1] 2 2 2

: :

x <- " One    Two    "
nchar(gsub("[^ ]|^ *| *$", "", gsub(" +", " ", x))) + 1 
# [1] 2
+2

All Articles