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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue, 29 Apr 2008 11:08:31 -0400

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.
>



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. Received on Tue 29 Apr 2008 - 16:57:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Apr 2008 - 17:30:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive