[R] Merge or combine data frames with missing columns
Mark Heckmann
mark.heckmann at gmx.de
Mon Dec 29 22:35:07 CET 2008
thanks for the elegant solution!
It worked out fine in my case, but when I tried to understand what it does I discovered one little issue.
l <- list(data.frame(a=1, b=2), data.frame(a=2, c=3))
do.call(rbind, lapply(l, "[", unique(unlist(sapply(l, names)))))
will not work. Neither the following list.
l <- list(data.frame(a=1, b=2), data.frame(a=2, c=3, d=5))
I am not able to figure out why. Does anyone know what could be done to fix it?
TIA, Mark
-------- Original-Nachricht --------
> Datum: Mon, 29 Dec 2008 16:17:22 -0200
> Von: "Henrique Dallazuanna" <wwwhsd at gmail.com>
> An: "Mark Heckmann" <mark.heckmann at gmx.de>
> CC: r-help at r-project.org
> Betreff: Re: [R] Merge or combine data frames with missing columns
> Try this:
>
> do.call(rbind, lapply(l, "[", unique(unlist(sapply(l, names)))))
>
> Where "l" is your list.
>
> On Mon, Dec 29, 2008 at 12:18 PM, Mark Heckmann
> <mark.heckmann at gmx.de>wrote:
>
> >
> > Hi R-experts,
> >
> > suppose I have a list with containing data frame elements:
> >
> > [[1]]
> > (Intercept) y1 y2 y3 y4
> > -6.64 0.761 0.383 0.775 0.163
> >
> > [[2]]
> > (Intercept) y2 y3
> > -3.858 0.854 0.834
> >
> > Now I want to put them into ONE dataframe like this:
> > (Intercept) y1 y2 y3
> y4
> > 1 -6.64 0.761 0.383 0.775 0.163
> > 2 -3.858 NA 0.854 0.834 NA
> >
> > The problem I encounter is that not always all possible columns
> (Intercept,
> > y1, y2, y3, y4) exist or at least have NA entries .
> >
> > I cannot figure out how (e.g. with merge)...
> >
> > TIA, Mark
> >
> >
> > --
> > Mark Heckmann (Dipl. Wirt.-Ing.)
> > phone +49 (0) 421/1614618
> >
> > Sensationsangebot verlängert: GMX FreeDSL - Telefonanschluss + DSL
> > für nur 16,37 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K1308T4569a
> >
> > ______________________________________________
> > 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.
> >
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
--
Mark Heckmann (Dipl. Wirt.-Ing.)
phone +49 (0) 421/1614618
More information about the R-help
mailing list