rip_lahey
rip_lahey

Reputation: 1

How do I extract full strings that start with a pattern in R

I have data that contains a list of tweets like so:

"what is up", "RT @lolol wassup", "RT @joe pls help me"

I want to be able to extract the strings that start with RT @, and store it in another list. I'm currently using this:

str_extract(data, "^RT[:space:]+@[:graph:]+")

But that only extracts the "RT @name" part of the string, not the entire tweet. Can't figure out what's wrong with the regex. Thank you for the help!

Upvotes: 0

Views: 963

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389335

str_extract extracts the pattern you specify. To get the complete tweet when a pattern is supplied use str_subset.

library(stringr)
result <- str_subset(data, "^RT[:space:]+@[:graph:]+")
result
#[1] "RT @lolol wassup"    "RT @joe pls help me"

Upvotes: 1

akrun
akrun

Reputation: 887981

We can use grep to extract the full string that starts (^) with 'RT' followed by a space and @ in base R

grep('^RT @', data, value = TRUE)
#[1] "RT @lolol wassup"    "RT @joe pls help me"

data

data <- c("what is up", "RT @lolol wassup", "RT @joe pls help me")

Upvotes: 2

Related Questions