2016-11-06 75 views
0

我试图从数据集中删除转发(字符串以RT开头),但我的grepl命令似乎没有正确工作。使用grepl删除值

这工作得很好:

grepl("[^rt|RT][:alnum]",c("RT hi","rt boo","rtlolo","im goodRT"),ignore.case=T)

失败。为什么?

data<-structure(list(data = c("RT @4MySquad: This makes me sick!\\n#whiteprivilege\\n#BlackLivesMatter \\n#Policestate https:\\/\\/t.co\\/nDL0AHwWTd", 
           "RT @weaselzippers: D.C. Police Want Help Identifying #BlackLivesMatter Supporters Who Beat And Left Hero Marine For Dead\\u2026 https:\\/\\/t.co\\/tbmO\\u2026", 
           "RT @vicegandako: #PrayForMannyPacquiao #LoveWins", "\\Dig out of the binaries of right and wrong\\ - #BlackLivesMatter at Mizzou", 
           "Even Democrats think #Bernie 's ideas are unrealistiC#insane #UNLV #BigBangTheory #Hillary2016 #blacklivesmatter https:\\/\\/t.co\\/ITDyXoAvtK", 
           "RT @eelawl1966: Former NAACP President Ben Jealous endorses Bernie Sanders\\n#BlackLivesMatter #BLM #Bernie2016 \\n https:\\/\\/t.co\\/Qom1KMwLHs", 
           "#SayNoToHillary #NoMoreClintons #FeelTheBern #BernieSanders #BlackLivesMatter #Disabled4Bernie #Women4Bernie... https:\\/\\/t.co\\/I8F21ilJgv", 
           "RT @JoshuaMannery: #BlackLivesMatter \\ud83d\\udc4a\\ud83c\\udffd https:\\/\\/t.co\\/tcEITKKGhd", 
           "lang:und", "@FoxNews Did he not say, \\Yes\\? Hopefully this story won't gain traction bc it's not reflective of the #blacklivesmatter movement", 
           "President Barack Obama Is Doing Big Things With Cuba + #BlackLivesMatter https:\\/\\/t.co\\/6gEJreOiUc", 
           "RT @Uberarabic: \\u0644\\u0644\\u0639\\u0644\\u0645 \\u0639\\u0642\\u0648\\u0628\\u0629 \\u0627\\u0644\\u0645\\u062b\\u0644\\u064a\\u064a\\u0646 \\u0641\\u064a \\u062c\\u0645\\u064a\\u0639 \\u0627\\u0644\\u062f\\u0627\\u064a\\u0627\\u0646\\u0627\\u062a \\u0627\\u0644\\u0633\\u0645\\u0627\\u0648\\u064a\\u0629 \\u0647\\u064a \\u0627\\u0644\\u0642\\u062a\\u0644\\n\\n#LoveWins", 
           "RT @AishaYesufu: Let's not forget 219#ChibokGirls still in captivity today 676 days \\n#NeverToBeForgotten #CryingToBeRescued #BringBackOurGi\\u2026", 
           "RT @arctic_matters: Chukchi Sea. #LoveWins https:\\/\\/t.co\\/gH8KZgVZk3", 
           ". @DoubleFine r u joking, tim u know the servers aren't working you dumb asshole #gamergate", 
           "RT @realkingcalii: #BlackLivesMatter Kendrick Lamar \\Alright\\ - https:\\/\\/t.co\\/amlRn0fKsA", 
           "RT @DreamersMOMS: Community representing #CCA &amp; @geogroups making dirty $$$$ w\\/immigrants. #WeAreFlorida #not1more #immigration https:\\/\\/t.c\\u2026", 
           "id_str:700", "RT @DreamersMOMS: Con compa\\u00f1eras de Carolina del Norte apoy\\u00e1ndonos en #Tallahassee. #ProteccionNoDeportation #Not1More @grisalonso https:\\/\\/\\u2026", 
           "RT @IkeIsaacson2: Hey #blacklivesmatter this is a hate crime done by racists in your name. https:\\/\\/t.co\\/6uGSXAJcrM" 
)), .Names = "data", row.names = c(NA, 20L), class = "data.frame") 

data[grepl("[^rt|RT][:alnum]",data,ignore.case=T)] 

this question也使用Twitter的数据,但有一个不同的方法

+0

难道你的意思是'grepl( “^([RT | RT])[[:alnum:]] +”,C( “RT喜”, “RT嘘”, “rtlolo”,“IM goodRT “),ignore.case = TRUE)#[1] TRUE TRUE TRUE FALSE' – akrun

+0

我不想排除第三个字符串'rtlolo'。在我的情况下,它是有效的,因为它不以'(rt | RT)[\ s]开始' – Rilcon42

+0

如果我在上面的注释中使用了正则表达式,我只想排除以'rt'或'RT开头的推文'(注意最后的空格) – Rilcon42

回答

1

我们指定的模式,即开始后跟一个或多个空格(\\s+)字符(^)与RT和作为ignore.case = TRUE,它也将获得以rt开头的元素,然后是空格。

grepl("^RT\\s+",c("RT hi","rt boo","rtlolo","im goodRT"), ignore.case=TRUE) 
#[1] TRUE TRUE FALSE FALSE 

grep("^RT\\s+", data$data, ignore.case=TRUE, value = TRUE) 
#[1] "RT @4MySquad: This makes me sick!\\n#whiteprivilege\\n#BlackLivesMatter \\n#Policestate https:\\/\\/t.co\\/nDL0AHwWTd"                                                                 
#[2] "RT @weaselzippers: D.C. Police Want Help Identifying #BlackLivesMatter Supporters Who Beat And Left Hero Marine For Dead\\u2026 https:\\/\\/t.co\\/tbmO\\u2026"                                                       
#[3] "RT @vicegandako: #PrayForMannyPacquiao #LoveWins"                                                                                  
#[4] "RT @eelawl1966: Former NAACP President Ben Jealous endorses Bernie Sanders\\n#BlackLivesMatter #BLM #Bernie2016 \\n https:\\/\\/t.co\\/Qom1KMwLHs"                                                          
#[5] "RT @JoshuaMannery: #BlackLivesMatter \\ud83d\\udc4a\\ud83c\\udffd https:\\/\\/t.co\\/tcEITKKGhd"                                                                       
#[6] "RT @Uberarabic: \\u0644\\u0644\\u0639\\u0644\\u0645 \\u0639\\u0642\\u0648\\u0628\\u0629 \\u0627\\u0644\\u0645\\u062b\\u0644\\u064a\\u064a\\u0646 \\u0641\\u064a \\u062c\\u0645\\u064a\\u0639 \\u0627\\u0644\\u062f\\u0627\\u064a\\u0627\\u0646\\u0627\\u062a \\u0627\\u0644\\u0633\\u0645\\u0627\\u0648\\u064a\\u0629 \\u0647\\u064a \\u0627\\u0644\\u0642\\u062a\\u0644\\n\\n#LoveWins" 
#[7] "RT @AishaYesufu: Let's not forget 219#ChibokGirls still in captivity today 676 days \\n#NeverToBeForgotten #CryingToBeRescued #BringBackOurGi\\u2026"                                                         
#[8] "RT @arctic_matters: Chukchi Sea. #LoveWins https:\\/\\/t.co\\/gH8KZgVZk3"                                                                            
#[9] "RT @realkingcalii: #BlackLivesMatter Kendrick Lamar \\Alright\\ - https:\\/\\/t.co\\/amlRn0fKsA"                                                                       
#[10] "RT @DreamersMOMS: Community representing #CCA &amp; @geogroups making dirty $$$$ w\\/immigrants. #WeAreFlorida #not1more #immigration https:\\/\\/t.c\\u2026"                                                       
#[11] "RT @DreamersMOMS: Con compa\\u00f1eras de Carolina del Norte apoy\\u00e1ndonos en #Tallahassee. #ProteccionNoDeportation #Not1More @grisalonso https:\\/\\/\\u2026"                                                      
#[12] "RT @IkeIsaacson2: Hey #blacklivesmatter this is a hate crime done by racists in your name. https:\\/\\/t.co\\/6uGSXAJcrM"