Skip to content
Prev 259922 / 398502 Next

how to merge within range?

I'd've first said it's "simply"

sapply(df1$time, function(x) if(any(foo <- (x>=df2$from &
x<=df2$to))>0) df2$value[which(foo)] else NA )

but the following are much nicer (except that instead of NA you'll
have 0 but that's easy to change if necessary):

 colSums(sapply(df1$time, function(x) (x>=df2$from & x <=df2$to) * df2$value) )
 rowSums(outer(df1$time, df2$from, ">=") * outer(df1$time, df2$to,
"<=") * df2$value)



On Sat, May 14, 2011 at 10:08 PM, Ren? Mayer
<mayer at psychologie.tu-dresden.de> wrote: