-2

I am a confused linguist trying to use R to collect data from twitter. I have been using 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 specific character?

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

2 Answers2

2

There are lot of ways to do this. 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$')
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We can use stri_detect from stringi

library(stringi)
words[stri_detect(words, regex = 'r$')]
#[1] "tempr"    "infinitr" "twitter" 

Or with substring from base R

words[substring(words, nchar(words)) == 'r']

data

words <- c('rock', 'tempr', 'infinitr', 'end', 'twitter')
akrun
  • 874,273
  • 37
  • 540
  • 662