[R] merging multiple data frames with different numbers of rows

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 29 17:08:31 CEST 2008


It appears that these are time series (i.e. there is only one value of
River Mile in any one data frame) so if that's right you would be better
off representing them as time series.  Using zoo:

> library(zoo)
> feb06.z <- zoo(feb06[,2], feb06[,1])
> may06.z <- zoo(may06[,2], may06[,1])
> jun06.z <- zoo(jun06[,2], jun06[,1])
> merge(feb06.z, may06.z, jun06.z)
    feb06.z may06.z jun06.z
148      NA      NA      NA
185      NA      NA      NA
190      NA      NA      NA
198      NA      NA      NA
202 2850000      NA      NA

Alternately this also works:

> L <- list(feb06 = feb06, may06 = may06, jun06 = jun06)
> do.call("merge", lapply(L, function(x) zoo(x[,2], x[,1])))
      feb06 may06 jun06
148      NA    NA    NA
185      NA    NA    NA
190      NA    NA    NA
198      NA    NA    NA
202 2850000    NA    NA



Read the three vignettes that come with zoo and also ?merge.zoo

On Tue, Apr 29, 2008 at 10:42 AM, stephen sefick <ssefick at gmail.com> wrote:
> merge can only merge two objects at a time- I would like to merge more than
> two objects at a time.
>
> s.d <- structure(list(RiverMile = c(202L, 198L, 190L, 185L, 179L, 148L,
> 119L, 61L)), .Names = "RiverMile", row.names = c(NA, -8L), class =
> "data.frame")
> #s.d is all of the river miles that can occur in all of the data frames that
>
> I want to put together
>
> feb06 <- structure(list(RiverMile = c(202L, 190L, 185L), X2.1.06 =
> c(2850000,
> NA, NA)), .Names = c("RiverMile", "X2.1.06"), row.names = c(29L,
> 31L, 32L), class = "data.frame")
>
> may06 <- structure(list(RiverMile = c(202L, 198L, 190L, 185L, 148L), X5.1.06
>
> = c(NA_real_,
> NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("RiverMile",
> "X5.1.06"), row.names = c(29L, 30L, 31L, 32L, 34L), class = "data.frame")
>
> jun06 <- structure(list(RiverMile = c(202L, 198L, 190L, 185L, 148L), X6.1.06
> = c(NA_real_,
> NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("RiverMile",
> "X6.1.06"), row.names = c(29L, 30L, 31L, 32L, 34L), class = "data.frame")
>
> merge(s.d, feb06, all=T)
>  RiverMile X2.1.06
> 1        61      NA
> 2       119      NA
> 3       148      NA
> 4       179      NA
> 5       185      NA
> 6       190      NA
> 7       198      NA
> 8       202 2850000
>
> #this is what I want- is there a way to implement this over many data frames
>
> --
> Let's not spend our time and resources thinking about things that are so
> little or so large that all they really do for us is puff us up and make us
> feel like gods. We are mammals, and have not exhausted the annoying little
> problems of being mammals.
>
> -K. Mullis
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list