R - 如何删除某个短语之前和单个列中某个短语之后的行

时间:2017-05-02 01:12:39

标签: r rows data-cleaning

您好我想在某个短语之前删除行,然后在稍后出现的相同(几乎)短语之后删除。我想另一种看待它的方法是只保留某个部分开头和结尾的数据。

我的数据如下:

df <- data.frame(time = as.factor(c(1,2,3,4,5,6,7,8,9,10,11,12,13)), 
                 type = c("","","GMT:yyyy-mm-dd_HH:MM:SS_LT:2016-10-18_06:09:53","(K)","","","","(K)","(K)","","(K)","GMT:yyyy-mm-dd_HH:MM:SS_CAM:2016-10-18_06:20:03",""),
                 names = c("J","J","J","J","J","J","J","J","J","J","J","J","J"))

我希望删除第一个GMT:yyyy...词组之前和第二个GMT:yyyy...词组之后的所有内容。所以最终产品将是

time   type                                                    names
3      GMT:yyyy-mm-dd_HH:MM:SS_LT:2016-10-18_06:09:53           J
4      (K)                                                      J
5                                                               J
6                                                               J
7                                                               J 
8      (K)                                                      J
9      (K)                                                      J
10                                                              J
11     (K)                                                      J
12     GMT:yyyy-mm-dd_HH:MM:SS_LT:2016-10-18_06:20:03           J

我认为subset可能有效,但它给了我一些问题。

2 个答案:

答案 0 :(得分:2)

使用grep,您可以找到找到模式的行的索引:

ind=grep("^GMT",df$type)

然后,您只能保留两个索引之间的行:

df=df[ind[1]:ind[2],]

答案 1 :(得分:1)

library(tidyverse)
library(stringr)

df2 <- df %>% slice(str_which(type, "GMT")[1]:str_which(type, "GMT")[2])