Dear All, Debuting in R, I'm facing a problem. I have 2 vectors, say 'a' et 'b', and I'd like to merge them according to the proximity of their variable 'time'. How to do to keep elements which satisfy (for example) 'a$time-b$time<0.5'? For example :> atime x 1 1.0 4 2 2.2 5 3 5.2 6> btime y 1 0 1 2 1 3 3 2 5 4 4 7 5 5 9 I'd like to get :>time x y 1 1.0 4 3 2 2.2 5 5 3 5.2 6 9 I thought using the fonction 'merge'... I hope you can help me! Thanks in advance! Jerome. -- View this message in context: http://r.789695.n4.nabble.com/Merge-data-under-conditions-tp3350864p3350864.html Sent from the R help mailing list archive at Nabble.com.
On Mar 12, 2011, at 4:14 PM, flymer wrote:> Dear All, > > Debuting in R, I'm facing a problem. > I have 2 vectors, say 'a' et 'b', and I'd like to merge them > according to > the proximity of their variable 'time'. > How to do to keep elements which satisfy (for example) 'a$time-b > $time<0.5'? > > For example : > >> a > time x > 1 1.0 4 > 2 2.2 5 > 3 5.2 6 > >> b > time y > 1 0 1 > 2 1 3 > 3 2 5 > 4 4 7 > 5 5 9 > > I'd like to get : > >> > time x y > 1 1.0 4 3 > 2 2.2 5 5 > 3 5.2 6 9 > > I thought using the fonction 'merge'...There are often SQL magical incantation to acheive such, and there is an `sqldf` package that might help, but I am not competent with it. Here is a base R solution using three functions (six, if you count "$", "<", and "-": ?expand.grid ?rep ?"[" dfrm<- expand.grid(a$time, b$time) dfrm$x <- a$x # by virtue of recycling dfrm$y <- rep(b$y, each=3) > dfrm[abs(dfrm$Var1-dfrm$Var2) < 0.5, ] Var1 Var2 x y 4 1.0 1 4 3 8 2.2 2 5 5 15 5.2 5 6 9 -- David Winsemius, MD West Hartford, CT
use the sqldf package:> require(sqldf) > atime x 1 1.0 4 2 2.2 5 3 5.2 6> btime y 1 0 1 2 1 3 3 2 5 4 4 7 5 5 9> sqldf("+ select a.time, a.x, b.y + from a, b + where abs(a.time - b.time) < 0.5 + ") time x y 1 1.0 4 3 2 2.2 5 5 3 5.2 6 9>On Sat, Mar 12, 2011 at 4:14 PM, flymer <flymer at hotmail.fr> wrote:> Dear All, > > Debuting in R, I'm facing a problem. > I have 2 vectors, say 'a' et 'b', and I'd like to merge them according to > the proximity of their variable 'time'. > How to do to keep elements which satisfy (for example) 'a$time-b$time<0.5'? > > For example : > >> a > ?time x > 1 ?1.0 4 > 2 ?2.2 5 > 3 ?5.2 6 > >> b > ?time y > 1 ? ?0 1 > 2 ? ?1 3 > 3 ? ?2 5 > 4 ? ?4 7 > 5 ? ?5 9 > > I'd like to get : > >> > ?time x y > 1 ?1.0 4 3 > 2 ?2.2 5 5 > 3 ?5.2 6 9 > > I thought using the fonction 'merge'... > I hope you can help me! Thanks in advance! > > Jerome. > > > -- > View this message in context: http://r.789695.n4.nabble.com/Merge-data-under-conditions-tp3350864p3350864.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?