Re: [R] Merge or combine data frames with missing columns
Try this: do.call(rbind, lapply(lapply(l, unlist), [, unique(unlist(c(sapply(l, names)) On Mon, Dec 29, 2008 at 7:35 PM, Mark Heckmann mark.heckm...@gmx.de wrote: 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 www...@gmail.com An: Mark Heckmann mark.heckm...@gmx.de CC: r-help@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.heckm...@gmx.dewrote: 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.834NA 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@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 Pt! Schon vom neuen GMX MultiMessenger gehört? Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ R-help@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] Merge or combine data frames with missing columns
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.834NA 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@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.
Re: [R] Merge or combine data frames with missing columns
Mark, I think ?rbind should work for you. Regards, Brian -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Mark Heckmann Sent: Monday, December 29, 2008 9:18 AM To: r-help@r-project.org Subject: [R] Merge or combine data frames with missing columns 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.834NA 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@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. -- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. -- __ R-help@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.
Re: [R] Merge or combine data frames with missing columns
On Mon, 29 Dec 2008, Mark Heckmann 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.834NA 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)... Just use a nested for loop: my.list - list(c(a=1,b=2,c=3),c(a=10,b=20),c(g=7,h=8)) dat - data.frame() for(i in seq(along=my.list)) for(j in names(my.list[[i]])) dat[i,j] - my.list[[i]][j] If what you actually wanted was a _matrix_, and if it is rather large, faster solutions might be possible using tapply or sapply. HTH, Chuck 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@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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@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.
Re: [R] Merge or combine data frames with missing columns
How about this solution g1 - data.frame(ic = 1, y1 = 2, y2 = 3, y3 = 4, y4 = 5) g2 - data.frame(ic = 2, y2 = 6, y3 = 7) g - list(g1, g2) library(gregmisc) do.call(smartbind, g) -Lauri __ R-help@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.
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.heckm...@gmx.dewrote: 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.834NA 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@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 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Merge or combine data frames with missing columns
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 www...@gmail.com An: Mark Heckmann mark.heckm...@gmx.de CC: r-help@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.heckm...@gmx.dewrote: 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.834NA 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@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 __ R-help@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.
Re: [R] Merge or combine data frames with missing columns
Or even more simply: On Mon, Dec 29, 2008 at 12:17 PM, Henrique Dallazuanna www...@gmail.com wrote: Try this: do.call(rbind, lapply(l, [, unique(unlist(sapply(l, names) Where l is your list. Or even more simply: library(plyr) do.call(rbind.fill, l) which doesn't get the variable order quite right, but should in a future version. Hadley -- http://had.co.nz/ __ R-help@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.