如果以“RT”开头,请删除行

时间:2014-04-13 16:02:26

标签: regex r substring

如何删除那些以" RT"开头的行?在第一栏?

    structure(list(text = structure(c(4L, 6L, 1L, 2L, 5L, 3L), .Label = c("@AirAsia @AirAsiaId finally they let us fly with 9.20 flight today. Manual boarding pass. Phew, that was a great relief!", 
    "@AirAsia your direct debit (Maybank) payment gateways is not working. Is it something you are working to fix?", 
    "RT @AirAsia: Kindly note that CIMB Direct Debit service will be unavailable tonight from (GMT+8) 1145hrs on 31 Jan until 0600hrs on 3 Feb 2…", 
    "RT @AirAsia: Skipped breakfast this morning? Now you can enjoy a great breakfast onboard with our new breakfast meals! http://t.co/957ZaLjY…", 
    "xdek ke flight @AirAsia Malaysia to LA... hahah..bagi la promo murah2 sikit, kompom aku beli...", 
    "You know there is a problem when customer service asks you to wait for 103 minutes and your no is 42 in the queue. @AirAsia"
    ), class = "factor"), created = structure(c(5L, 4L, 4L, 3L, 2L, 
    1L), .Label = c("1/2/2014 16:14", "1/2/2014 17:00", "3/2/2014 0:54", 
    "3/2/2014 0:58", "3/2/2014 1:28"), class = "factor")), .Names = c("text", 
    "created"), class = "data.frame", row.names = c(NA, -6L))

4 个答案:

答案 0 :(得分:4)

假设您的数据框名为tweets,那么

no.rts <- tweets[grep("^RT ", tweets$text, invert=TRUE),]

将执行您想要的操作(并将结果放在名为no.rts的新数据框中。)

grep语句表示忽略tweets$text^RT开头的所有行。如果没有invert=TRUE,它将选择RT开头的所有行。

答案 1 :(得分:4)

grepl也有效。假设d是数据集,

> d[!grepl("^RT", d$text), ]
##                        text        created
## 2 You know there...@AirAsia  3/2/2014 0:58
## 3 @AirAsia... great relief!  3/2/2014 0:58
## 4 @AirAsia...orking to fix?  3/2/2014 0:54
## 5 xdek ke flight ...        1/2/2014 17:00

答案 2 :(得分:0)

或者使用stri_sub包中的stringi函数获取前两个字符,然后检查它们是否等于&#34; RT&#34;:

require(stringi)
df[stri_sub(df$text,1,2)!="RT",]

答案 3 :(得分:0)

以上所有工作,我更喜欢子集,因为它更容易辨认:

no.rts <- subset( tweets, ! grepl("^RT ", text) )
相关问题