我的数据如下:
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"
?
答案 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"
的行索引,:
,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