简体   繁体   中英

How can I count words in an observation in R

I need to count the words occurring in an observation. Some observations have only one word, but others have more than one and are separated by "/".

Here is a data set for a reproducible example.

data.1 <-read.csv(text = "
obs, a
2, date
3, Ari
4, MO
5, date/Ari
6, date/MO
7, Ari/MO
8, date/Ari/MO
")

This is the solution I came up with. But this solution is not counting correctly those observations with only one word.

data.2 <-  data.1 %>% mutate(n.words = lengths(gregexpr("/", a)) + 1)

This is the output

在此处输入图像描述

Any help will be really appreciated.

Thanks.

This worked for me:

lengths(strsplit(as.character(data.1$a),"/"))

We could use str_count from stringr

library(stringr)
library(dplyr)
data.1 %>% 
    mutate(n.words = str_count(a, "\\w+"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM