-2

i have 5 texts that get from the tweet. i wonder which one has a hashtag. Text what is the logical vector to find that?

Kuai Yu
  • 1
  • 3
  • Please provide sample data so that this problem is reproducible. Right now, it will be difficult for people to help you out. (We don't want to type all the stuff from your image into our R sessions :) – mysteRious Mar 27 '18 at 00:38
  • When you ask a question, you want to provide a reproducible data and a running code. Just throwing your text in a link does not help any SO users. Would you read [this link](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? Then, you will understand how you are supposed to ask questions. – jazzurro Mar 27 '18 at 00:41

2 Answers2

0

You can achieve this by

grepl("#", text)

Note, your vector of text data is assumed to be text. This will return a vector of TRUEs and FALSEs.

smanski
  • 541
  • 2
  • 7
0

You can get the indices of the tweets that match with grep

Tweets = c("sefgfg efgwre", "asdwfg #ABC erwthtr", "#wdfv efgv ertbv",
    "wetg rth fgwetgr", "adsfd v verfwf df #ghqwef")
grep("#\\w", Tweets)
[1] 2 3 5

If you want to get what the hashtags are, you can do this in two steps with regexpr and regmatches

m = regexpr("#\\S+", Tweets)
regmatches(Tweets, m)
[1] "#ABC"    "#wdfv"   "#ghqwef"
G5W
  • 36,531
  • 10
  • 47
  • 80