Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting specified word from a vector using R

Tags:

r

I have a text e.g

text<- "i am happy today :):)"

I want to extract :) from text vector and report its frequency

like image 855
jan5 Avatar asked Apr 24 '26 03:04

jan5


1 Answers

Here's one idea, which would be easy to generalize:

text<- c("i was happy yesterday :):)",
         "i am happy today :)",
         "will i be happy tomorrow?")

(nchar(text) - nchar(gsub(":)", "", text))) / 2
# [1] 2 1 0
like image 77
Josh O'Brien Avatar answered Apr 26 '26 16:04

Josh O'Brien