使用dplyr过滤从一个变量到另一个变量的所有行

时间:2017-04-20 02:23:11

标签: r dplyr

我的数据如下:

data <- data.frame(
  value = runif(10)
  id = c("junk","start","1","2","end","morejunk","junk","start","4","end")
)

我想使用filter()id "start"id "end"提取所有内容。问题是起始行和结束行之间的观察数量不同,因此我不能过滤每x行。有没有办法以我可以指定filter() from = "start"的方式使用until = "end"

2 个答案:

答案 0 :(得分:1)

您可以先确定“开始”和“结束”的位置。然后使用那些成对的索引来索引data.frame。这假设每次都有一对相应的开始和结束。

set.seed(0L)
data <- data.frame(
    value = runif(10),
    id = c("junk","start","1","2","end","morejunk","junk","start","4","end")
)
idx <- which(data$id %in% c("start", "end"))
lapply(split(idx, ceiling(seq_along(idx)/2)), function(x) data[x[1]:x[2],])

答案 1 :(得分:1)

你可以

  • 使用which标识"start""end"的行索引,
  • 分别从每个中加1和减1,以便不包括这些行
  • 通过:
  • 将这些序列并行传递到Map
  • unlist将列表简化为矢量,
  • 子集slice

离开

library(dplyr)
set.seed(47)

data <- data.frame(
    value = runif(10),
    id = c("junk","start","1","2","end","morejunk","junk","start","4","end")
)

data %>% slice(unlist(Map(`:`, 
                          which(.$id == 'start') + 1, 
                          which(.$id == 'end') - 1)))
#> # A tibble: 3 × 2
#>       value     id
#>       <dbl> <fctr>
#> 1 0.7615020      1
#> 2 0.8224916      2
#> 3 0.5433097      4

或在基地,

data[unlist(Map(`:`, 
                which(data$id == 'start') + 1, 
                which(data$id == 'end') - 1)), ]
#>       value id
#> 3 0.7615020  1
#> 4 0.8224916  2
#> 9 0.5433097  4