Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to use R to collect data from twitter. I have been working with the package twitteR and it has been working pretty well with fixed strings, but I'd like to ask it to get tweets that include "querendo + infinitive verb". In Portuguese, verbs in the infinitive always end with the character 'r'. How can I ask for words that end with a particular character?

searchtwitteR(" ", n = 1000, lang = pt, locate = Brazil)

1 Answer

0 votes
by (108k points)

For that, you can do that in many ways. Consider words as a vector

words <- c('rock', 'tempr', 'infinitr', 'end', 'twitter')

In base R :

1) Using endsWith

words[endsWith(words, 'r')]

#[1] "tempr"    "infinitr" "twitter" 

2) Using grep

grep('r$', words, value = TRUE)

3) grepl

words[grepl('r$', words)]

Using stringr :

library(stringr)

1) str_detect

words[str_detect(words, 'r$')]

2) str_subset

str_subset(words, 'r$')

If you are a beginner and want to know more about R then do check out the R programming course

Browse Categories

...