[英]Using dplyr to filter all rows from one variable until another
我的數據如下所示:
data <- data.frame(
value = runif(10)
id = c("junk","start","1","2","end","morejunk","junk","start","4","end")
)
我想使用filter()
從id
"start"
到id
"end"
提取所有內容。 問題是開始行和結束行之間的觀察次數不同,因此我無法篩選每x行。 有沒有一種方法可以我可以from = "start"
until = "end"
來指定使用filter()
?
您可以首先確定“開始”和“結束”的位置。 然后使用這些成對的索引來索引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],])
您可以
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.