[R] Combine 'overlapping' dataframes, respecting row names
James Howison
jhowison at syr.edu
Thu Aug 31 06:36:16 CEST 2006
Hi,
I've examined the archives and found quite a few questions on
concatenating dataframes, but none that really addressed my issue,
I'm afraid. I've also examined the cbind and rbind documentation but
nonetheless here I am writing to r-help ;)
This is what I have (the row names are dates used for conversion to
an irregular time series with the its package):
> cvsFrame
cvsactions
2002-11-15 4
2002-12-15 9
2003-01-15 5
2003-02-15 5
> downloadsFrame
downloads
2002-09-15 1
2002-10-15 2
2002-11-15 12
2002-12-15 8
(notice how the dates are overlapping?)
The output I'd like is:
cvsaction downloads
2002-09-15 NA 1
2002-10-15 NA 2
2002-11-15 4 12
2002-12-15 9 8
2003-01-15 5 NA
2003-02-15 5 NA
ie. merge the data.frames, respecting the row.names and inserting NAs
where a frame didn't contain info for a row in the final frame.
This is the closest I gotten (I'm sure cbind is doing what it's meant
to do but it's obviously not what I need)
> cbind(downloadsFrame,cvsFrame)
downloads cvsactions
2002-09-15 1 4
2002-10-15 2 9
2002-11-15 12 5
2002-12-15 8 5
It takes the row.names from the first frame given and then just adds
the data in rows 1 through 4, regardless of their row.name. And it
doesn't work at all if the column lengths are different. (Yes, it
would be nice if the 'its' class had a way to merge 'its' objects,
but the question seemed general enough to ask on list.)
Thanks,
James
More information about the R-help
mailing list