title: Generating graphs of retweets and mentions on twitter with R
body: <p>I'm trying to analyse a retweet and a mention graph network with R. </p>
  
  <p><strong>My Problem:</strong>
  I try to create data frames which include "sender" and "receiver" of the retweets/mentions. The code works very well with most of the tweets, but sometimes the "receiver" column not only includes the names but also the rest of the text. How do I fix this? </p>
  
  <p><strong>This ist my code:</strong> </p>
  
  <pre><code>    # Get @-messages, senders, receivers
  ats &lt;- grep("^\\.?@[a-z0-9_]{1,15}", tolower(munich_data_500$text_clean), perl=T, value=T);
  at.sender &lt;- tolower(as.character(munich_data_500$screen_name[grep("^\\.?@[a-z0-9_]{1,15}", tolower(munich_data_500$text_clean), perl=T)]));
  at.receiver &lt;- gsub("^\\.?@([a-z0-9_]{1,15})[^a-z0-9_]+.*$", "\\1", ats, perl=T);
  print(paste(length(ats), " @-messages from ", length(unique(at.sender)), " senders and ", length(unique(at.receiver)), " receivers.", sep=""));
  
  
  # Get RTs, senders, receivers
  rts &lt;- grep("^rt @[a-z0-9_]{1,15}", tolower(munich_data_500$text_clean), perl=T, value=T);
  rt.sender &lt;- tolower(as.character(munich_data_500$screen_name[grep("^rt @[a-z0-9_]{1,15}", tolower(munich_data_500$text_clean), perl=T)]));
  rt.receiver &lt;- gsub("^rt @([a-z0-9_]{1,15})[^a-z0-9_]+.*$", "\\1", rts, perl=T);
  print(paste(length(rts), " RTs from ", length(unique(rt.sender)), " senders and ", length(unique(rt.receiver)), " receivers.", sep=""));
  
  
  # This is necessary to avoid problems with empty entries, usually caused by encoding issues in the source files
  at.sender[at.sender==""] &lt;- "";
  at.receiver[at.receiver==""] &lt;- "";
  rt.sender[rt.sender==""] &lt;- "";
  rt.receiver[rt.receiver==""] &lt;- "";
  
  # Create a data frame from the sender-receiver information
  ats.df &lt;- data.frame(at.sender, at.receiver);
  rts.df &lt;- data.frame(rt.sender, rt.receiver);
  </code></pre>
  
  <p><strong>Code Source</strong></p>
  
  <p><a href="https://www.r-bloggers.com/generating-graphs-of-retweets-and-messages-on-twitter-using-r-and-gephi/" rel="nofollow noreferrer">https://www.r-bloggers.com/generating-graphs-of-retweets-and-messages-on-twitter-using-r-and-gephi/</a></p>
  
  <p><strong>A Copy of my data</strong> </p>
  
  <pre><code>structure(list(user_id_str = c("4193500174", "2198130404", "455851683", 
  "2316380063", "3192451", "2372023345"), screen_name = c("unterdembettRU", 
  "NejlaBevab", "MatzePirwitz", "nowakberlin", "leyrer", "RockiDave"
  ), name = c("Amnesia Anderson", "nejla bevab", "marcel pirwitz", 
  "Amaruq", "Martin Leyrer", "dave rocki"), description = c("Eine 1 Mann Armee die jeden zerfickt #pravibalkanci since 2016  backup @hurensohn8811", 
  "snapchat:nejlabevab      Instagram; nejla_wiebke", NA, NA, "Mitglied der demokratisch nicht legitimierten Internetgemeinde, CCC Wien, here private, ask before quoting me offline, Whitehat-Troll, OE1LYA", 
  "TRUTHER; ANTI NWO; ANTI ILLUMINATI;"), created_at = c("Sat Jul 23 15:41:46 +0000 2016", 
  "Sat Jul 23 15:41:46 +0000 2016", "Sat Jul 23 15:41:47 +0000 2016", 
  "Sat Jul 23 15:41:52 +0000 2016", "Sat Jul 23 15:41:57 +0000 2016", 
  "Sat Jul 23 15:41:57 +0000 2016"), id_str = c("756876968612732928", 
  "756876971875897344", "756876974384091136", "756876996366532610", 
  "756877014758518784", "756877015173697540"), url = c(NA, NA, 
  NA, "https://www.welt.de/politik/ausland/article157243137/Europa-nicht-in-der-Lage-seine-Buerger-zu-schuetzen.html", "https://www.welt.de/newsticker/news2/article157245377/Von-der-Leyen-Einsatz-der-Bundeswehr-in-Muenchen-wurde-erwogen.html", 
  NA), expanded_url = c(NA, NA, "https://twitter.com/kippi666/status/756592450450382853", 
  "http://www.welt.de/157243137", "http://www.welt.de/newsticker/news2/article157245377/Von-der-Leyen-Einsatz-der-Bundeswehr-in-Muenchen-wurde-erwogen.html", 
  NA), followers_count = c(290, 18, 2, 35, 3278, 332), statuses_count = c(3526, 
  266, 7, 3411, 115589, 4543), lang = c("de", "de", "de", "de", 
  "de", "de"), text_clean = c("ich ficke den #muenchen in den arsch !", 
  "rt @bmi_bund: #demaizieÌ€re ist jetzt auf dem weg nach #muenchen, um sich vor ort ein bild zu machen. ", 
  "rt @nielsruf: du wixa, du arschloch, du ", 
  "rt @rudisagmal: #muenchen #wuerzburg #nizza \n\n\"europa nicht in der lage, seine buÌˆrger zu schuÌˆtzen\"\n ", 
  "rt @metronaut: es brechen wirklich alle daÌˆmme: die regierung erwog ernsthaft beim amoklauf die bundeswehr im innern einzusetzen¦", 
  "rt @mhoepflinger: #hamburg #muenchen #ard\nblut an ihren haÌˆnden! angela merkel!\nniemand glaubt mehr an einen ruÌˆcktritt der diktatorin!\nhttpsâ€¦"
  )), .Names = c("user_id_str", "screen_name", "name", "description", 
  "created_at", "id_str", "url", "expanded_url", "followers_count", 
  "statuses_count", "lang", "text_clean"), row.names = c(NA, 6L
  ), class = "data.frame") 
  </code></pre>
  
  <p>(I had to delete the shortlinks in the tweets, I hope the copy still works)</p>
  
  <p><strong>System information</strong>
  platform       x86_64-w64-mingw32<br>
  arch           x86_64<br>
  os             mingw32<br>
  system         x86_64, mingw32<br>
  status<br>
  major          3<br>
  minor          4.1<br>
  year           2017<br>
  month          06<br>
  day            30<br>
  svn rev        72865<br>
  language       R<br>
  version.string R version 3.4.1 (2017-06-30)
  nickname       Single Candle  </p>