R 子集数据。帧并集多个间隔
考虑以下R 子集数据。帧并集多个间隔,r,R,考虑以下data.frame d <- data.frame(x = seq(0, 10, length=100), value = rnorm(100)) 现在,测试功能似乎是多余的,因为它看起来非常像内置的findInterval,但我找不到一种优雅的方法来使用它 condition <- Reduce(`|`, lapply(lapply(intervals, findInterval, x=d$
data.frame
d <- data.frame(x = seq(0, 10, length=100), value = rnorm(100))
现在,测试功能似乎是多余的,因为它看起来非常像内置的findInterval
,但我找不到一种优雅的方法来使用它
condition <- Reduce(`|`, lapply(lapply(intervals, findInterval,
x=d$x, all.inside=FALSE), `==`, 1))
d[condition, ]
条件
编辑:使用findInterval
d[findInterval(d$x,unlist(intervals))%%2==1,]
下面是一个使用interval
包的解决方案
d <- data.frame(x = seq(0, 10, length=100), value = rnorm(100))
intervals <- list(c(0.2, 0.8), c(1, 2), c(8, 8.2))
library(intervals)
intervals <- Intervals( do.call( rbind, intervals ) )
intervals <- reduce( intervals ) # Simplify, if they overlap
condition <- distance_to_nearest(d$x, intervals) == 0
# The following would allow for non-closed intervals,
# but it is awfully slow.
condition <- sapply( d$x, function(u)
any(!empty(interval_intersection( Intervals(c(u,u)), intervals ))))
d[condition,]
@baptiste OK,添加了一个findInterval
solutionnice,它还消除了for
循环在Reduce
d[findInterval(d$x,unlist(intervals))%%2==1,]
d <- data.frame(x = seq(0, 10, length=100), value = rnorm(100))
intervals <- list(c(0.2, 0.8), c(1, 2), c(8, 8.2))
library(intervals)
intervals <- Intervals( do.call( rbind, intervals ) )
intervals <- reduce( intervals ) # Simplify, if they overlap
condition <- distance_to_nearest(d$x, intervals) == 0
# The following would allow for non-closed intervals,
# but it is awfully slow.
condition <- sapply( d$x, function(u)
any(!empty(interval_intersection( Intervals(c(u,u)), intervals ))))
d[condition,]
intervals <- list(c(0.2, 0.8), c(1, 2), c(8, 8.2))
condition <- findInterval( d$x, unlist(intervals) ) %% 2 == 1
d[condition,]