Попробуйте это ...
Я создал образец кадра данных (но это всегда полезно, если вы включаете воспроизводимый пример).
some_nums <- c("2","100","16","999", "65")
the_words <- c("some words", "these are some more words", "and these are even more words too", "now fewer words", "I do not even want to try and count the number of words here so why not just let our code figure it out")
my_df <- data.frame(some_nums, the_words,stringsAsFactors = FALSE)
Вывод такой:
some_nums the_words
1 2 some words
2 100 these are some more words
3 16 and these are even more words too
4 999 now fewer words
5 65 I do not even want to try and count the number of words here so why not just let our code figure it out
Теперь нам просто нужно посчитать количество слов, применив функцию разделения строк к каждой из строк в нужном столбце нашего информационного кадра. Это можно сделать, используя пробел между словами в качестве нашего разделителя или то, что разделяет каждое слово. Мы также можем легко вставить эти значения в новый столбец на том же шаге, используя приведенный ниже код.
my_df[["number_of_words"]] <- sapply(strsplit(my_df$the_words, " "), length)
Получив следующий вывод:
some_nums the_words number_of_words
1 2 some words 2
2 100 these are some more words 5
3 16 and these are even more words too 7
4 999 now fewer words 3
5 65 I do not even want to try and count the number of words here so why not just let our code figure it out 24
Надеюсь, это поможет.