r - LEFT OUTER JOIN in sqldf with WHERE and HAVING clauses -


i have 2 data frames in r , conditionally merge them on id , day. merge right variables merged left variables new/fresh/recent possible, must @ least 3 days old.

but, if there isn't match in right id-date pair in left i'd still retain them. study has 2 parts, don't want drop id-day observations because they're not complete.

can in 1 sqldf step? current approach requires additional base r merge.

left <- data.frame(id=rep(1:5, each=10),                    day=rep(1:10, times=5),                    x=rnorm(5*10)) right <- data.frame(id=rep(1:2, each=21),                    day=rep(-10:10, times=2),                    y=rnorm(2*21)) combined <- sqldf("select l.id, l.day, l.x, r.y                   left l left outer join right r                   on (l.id = r.id)                   ((l.day - r.day) >= 3)                   group l.id, l.day                   having (r.day = max(r.day))") combined                    combined.2 <- merge(left, combined, all=true) combined.2  

try nesting select statements this:

sqldf("select * left        left join (select id, l.day, l.x, r.y                   left l left outer join right r                   using (id)                   ((l.day - r.day) >= 3)                   group l.id, l.day                   having (r.day = max(r.day)))         using (id, day, x)") 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -