Re: [R] rbind with different columns
Works wonderfully. I am very happy that I eventually found this post :) Daniel. -- View this message in context: http://r.789695.n4.nabble.com/rbind-with-different-columns-tp907370p2263588.html Sent from the R help mailing list archive at Nabble.com. __ 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] rbind with different columns
Karl Ove Hufthammer wrote: In article 4addc1d0.2040...@yahoo.de, niederlein-rs...@yahoo.de says... In every list entry is a data.frame but the columns are only partially the same. If I have exactly the same columns, I could do the following command to combine my data: do.call(rbind, myList) but of course it does not work with differnt column names. Is there any easy way to retrieve a combined table like this: You're in luck. 'rbind.fill' in the 'plyr' package does exactly what you want. Thanks a lot for your hint. I'll give it a look :-) So far, I have solved it with basic tools like this (it's less work than I thought): availableColumns - unique(unlist(sapply(myList, names))) myList - lapply(myList, FUN = function(entry) { missingColumns - availableColumns[which(!availableColumns %in% names(entry))] entry[, missingColumns] - NA entry }) do.call(rbind, myList) __ 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] rbind with different columns
Hello there, with the following dummy code I'd like to give you an idea how my data looks like: df1 - data.frame(a = rnorm(10), b = rnorm(10)) df2 - data.frame(a = rnorm(10), c = rnorm(10)) df3 - data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10)) myList - list(df1, df2, df3) # (myList is representing the data structure I have to handle, it's read from single files with lapply) In every list entry is a data.frame but the columns are only partially the same. If I have exactly the same columns, I could do the following command to combine my data: do.call(rbind, myList) but of course it does not work with differnt column names. Is there any easy way to retrieve a combined table like this: a b c 1 -0.54586587 -0.3607873 NA 2 1.10876842 1.1439414 NA 3 0.57357988 -1.2117743 NA 4 -1.40975759 -1.2390305 NA 5 0.03371093 -1.8860748 NA 6 -1.27682961 0.9990840 NA 7 -1.78715858 0.8400642 NA 8 -0.22663310 1.5224016 NA 9 0.45703787 0.0599217 NA 10 -1.21984635 1.1991689 NA 11 -2.58848301 NA 0.2394272 12 0.71155177 NA -0.7107332 13 -2.16440676 NA -0.1744845 14 1.33043121 NA 0.5951272 15 1.51034297 NA 0.1956956 16 1.00844947 NA 0.6726101 17 0.78693840 NA 1.2189904 18 0.68622170 NA 1.2230500 19 -1.09376863 NA 0.4267472 20 2.23647873 NA 0.7328574 21 -0.38144792 0.1532647 1.4824618 22 0.27078024 -0.4264737 0.1317450 23 1.10812086 1.2550117 0.1677935 24 0.14881701 -0.2928157 -1.4081529 25 -1.00635045 -0.7885968 -0.3502532 26 0.32024094 0.4681016 -1.5477557 27 0.82974710 0.2345186 -0.6572728 28 0.49608133 1.7463265 0.6493405 29 -0.33022738 1.9510503 -1.7930093 30 -0.62615365 0.7330671 -0.4032405 The only thing I can think about is checking the names of each list entry and adding NA-columns before combining them. Is there any other way to do so? Antje __ 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] rbind with different columns
In article 4addc1d0.2040...@yahoo.de, niederlein-rs...@yahoo.de says... In every list entry is a data.frame but the columns are only partially the same. If I have exactly the same columns, I could do the following command to combine my data: do.call(rbind, myList) but of course it does not work with differnt column names. Is there any easy way to retrieve a combined table like this: You're in luck. 'rbind.fill' in the 'plyr' package does exactly what you want. -- Karl Ove Hufthammer __ 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] rbind with different columns
Nice! I was going to recommend merge(merge(myList[[1]], myList[[2]], all=TRUE, sort=FALSE), myList[[3]], all=TRUE, sort=FALSE) but rbind.fill is better. -Ista On Tue, Oct 20, 2009 at 10:05 AM, Karl Ove Hufthammer k...@huftis.org wrote: In article 4addc1d0.2040...@yahoo.de, niederlein-rs...@yahoo.de says... In every list entry is a data.frame but the columns are only partially the same. If I have exactly the same columns, I could do the following command to combine my data: do.call(rbind, myList) but of course it does not work with differnt column names. Is there any easy way to retrieve a combined table like this: You're in luck. 'rbind.fill' in the 'plyr' package does exactly what you want. -- Karl Ove Hufthammer __ 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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ 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.