考虑以下data.frame

d <- data.frame(x = seq(0, 10, length=100), value = rnorm(100))


我希望基于属于以下任意间隔的x进行子集化,

intervals <- list(c(0.2, 0.8), c(1, 2), c(8, 8.2))

test <- function(range, x){
  which(x >= range[1] & x <= range[2])
}

d[Reduce(`union`, lapply(intervals, test, x=d$x)), ]


现在,测试功能似乎是多余的,因为它看起来很像内置的findInterval,但是我找不到一种优雅的使用方式。

condition <- Reduce(`|`, lapply(lapply(intervals,  findInterval,
                                       x=d$x, all.inside=FALSE), `==`, 1))

d[condition, ]


你能建议更好吗?

最佳答案

d[unlist(sapply(intervals, function(x) which(!is.na(cut(d$x,x))))),]
           x       value
3  0.2020202  0.15488314
4  0.3030303 -0.06891842
5  0.4040404  1.59909655
6  0.5050505  0.31006866
7  0.6060606  1.68986821
8  0.7070707  0.18500635
11 1.0101010  0.18721091
12 1.1111111  0.32485063
13 1.2121212 -0.42728405
14 1.3131313  0.84220081
15 1.4141414 -1.30745237
16 1.5151515 -1.90335389
17 1.6161616 -0.47139683
18 1.7171717  0.01622827
19 1.8181818  0.76362918
20 1.9191919 -0.37827765
81 8.0808081  0.46672521
82 8.1818182  1.27038641


编辑:使用findInterval的相同结果

d[findInterval(d$x,unlist(intervals))%%2==1,]

10-04 11:33