Hi all, I have a question about combining two data frames. For example, there are the two dataframes below, with the same structure but different column names and column lengths. How to add the values in DF2 to the end of DF1, though the column names do not match? How to add more than two? Thanks. DF1 year month day product1 product2 product3 1981 1 1 18 56 20 1981 1 2 19 45 22 1981 1 3 16 48 28 1981 1 4 19 50 21 DF2 yr mon d prod prod2 prod3 1981 2 1 17 49 25 1981 2 2 20 47 23 1981 2 3 21 52 27 I use the code below but it does not work. require(dplyr) bind_rows(DF1, DF2) or bind_cols(DF1, DF2) [[alternative HTML version deleted]]
Is there a reason not to just rename the columns? First, you should use dput(DF1) and dput(DF2) to send your example tables to the list: DF1 <- structure(list(year = c(1981L, 1981L, 1981L, 1981L), month = c(1L, 1L, 1L, 1L), day = 1:4, product1 = c(18L, 19L, 16L, 19L), product2 = c(56L, 45L, 48L, 50L), product3 = c(20L, 22L, 28L, 21L)), .Names = c("year", "month", "day", "product1", "product2", "product3"), class = "data.frame", row.names = c(NA, -4L)) DF2 <- structure(list(yr = c(1981L, 1981L, 1981L), mon = c(2L, 2L, 2L ), d = 1:3, prod = c(17L, 20L, 21L), prod2 = c(49L, 47L, 52L), prod3 = c(25L, 23L, 27L)), .Names = c("yr", "mon", "d", "prod", "prod2", "prod3"), class = "data.frame", row.names = c(NA, -3L )) Assuming they are the same structure as you said: colnames(DF2) <- colnames(DF1) rbind(DF1, DF2) # year month day product1 product2 product3 # 1 1981 1 1 18 56 20 # 2 1981 1 2 19 45 22 # 3 1981 1 3 16 48 28 # 4 1981 1 4 19 50 21 # 5 1981 2 1 17 49 25 # 6 1981 2 2 20 47 23 # 7 1981 2 3 21 52 27 The attached .png image file shows you how to send plain text emails to r-help using gmail. ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of lily li Sent: Wednesday, May 24, 2017 12:30 PM To: R mailing list <r-help at r-project.org> Subject: [R] about combining two dataframes Hi all, I have a question about combining two data frames. For example, there are the two dataframes below, with the same structure but different column names and column lengths. How to add the values in DF2 to the end of DF1, though the column names do not match? How to add more than two? Thanks. DF1 year month day product1 product2 product3 1981 1 1 18 56 20 1981 1 2 19 45 22 1981 1 3 16 48 28 1981 1 4 19 50 21 DF2 yr mon d prod prod2 prod3 1981 2 1 17 49 25 1981 2 2 20 47 23 1981 2 3 21 52 27 I use the code below but it does not work. require(dplyr) bind_rows(DF1, DF2) or bind_cols(DF1, DF2) [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. -------------- next part -------------- A non-text attachment was scrubbed... Name: GmailPlainText.png Type: image/png Size: 15966 bytes Desc: GmailPlainText.png URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20170524/e353db6c/attachment.png>
Hi Lily, maybe you should read up on what bind_rows/bind_cols (or the base functions rbind and cbind) do. bind_cols and cbind will fail in this case because of the different number of rows. bind_rows and rbind will fail because the column names are different - how can R know that month and mon really is the same. Depending on what you want, you should unify the column names (I have a hunch that this is what you want), or make sure the data.frames have the same number of rows. HTH Ulrik On Wed, 24 May 2017 at 19:30 lily li <chocold12 at gmail.com> wrote:> Hi all, > > I have a question about combining two data frames. For example, there are > the two dataframes below, with the same structure but different column > names and column lengths. How to add the values in DF2 to the end of DF1, > though the column names do not match? How to add more than two? Thanks. > > DF1 > year month day product1 product2 product3 > 1981 1 1 18 56 20 > 1981 1 2 19 45 22 > 1981 1 3 16 48 28 > 1981 1 4 19 50 21 > > DF2 > yr mon d prod prod2 prod3 > 1981 2 1 17 49 25 > 1981 2 2 20 47 23 > 1981 2 3 21 52 27 > > I use the code below but it does not work. > require(dplyr) > bind_rows(DF1, DF2) or bind_cols(DF1, DF2) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Thanks for your reply. I created the two dataframes (just numbers from txt files) in one for loop, so that it is confused to give them the same column names. That is the reason that I give them different column names to differentiate them, but it causes difficulty in later combining them. On Wed, May 24, 2017 at 11:42 AM, Ulrik Stervbo <ulrik.stervbo at gmail.com> wrote:> Hi Lily, > > maybe you should read up on what bind_rows/bind_cols (or the base > functions rbind and cbind) do. > > bind_cols and cbind will fail in this case because of the different number > of rows. > > bind_rows and rbind will fail because the column names are different - how > can R know that month and mon really is the same. > > Depending on what you want, you should unify the column names (I have a > hunch that this is what you want), or make sure the data.frames have the > same number of rows. > > HTH > Ulrik > > > > On Wed, 24 May 2017 at 19:30 lily li <chocold12 at gmail.com> wrote: > >> Hi all, >> >> I have a question about combining two data frames. For example, there are >> the two dataframes below, with the same structure but different column >> names and column lengths. How to add the values in DF2 to the end of DF1, >> though the column names do not match? How to add more than two? Thanks. >> >> DF1 >> year month day product1 product2 product3 >> 1981 1 1 18 56 20 >> 1981 1 2 19 45 22 >> 1981 1 3 16 48 28 >> 1981 1 4 19 50 21 >> >> DF2 >> yr mon d prod prod2 prod3 >> 1981 2 1 17 49 25 >> 1981 2 2 20 47 23 >> 1981 2 3 21 52 27 >> >> I use the code below but it does not work. >> require(dplyr) >> bind_rows(DF1, DF2) or bind_cols(DF1, DF2) >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >> >[[alternative HTML version deleted]]
Thanks, I didn't know the email function before. The code works. I found that using "=" is different from using "<-". On Wed, May 24, 2017 at 11:41 AM, David L Carlson <dcarlson at tamu.edu> wrote:> Is there a reason not to just rename the columns? > > First, you should use dput(DF1) and dput(DF2) to send your example tables > to the list: > > DF1 <- structure(list(year = c(1981L, 1981L, 1981L, 1981L), month = c(1L, > 1L, 1L, 1L), day = 1:4, product1 = c(18L, 19L, 16L, 19L), product2 = c(56L, > 45L, 48L, 50L), product3 = c(20L, 22L, 28L, 21L)), .Names = c("year", > "month", "day", "product1", "product2", "product3"), class = "data.frame", > row.names = c(NA, -4L)) > > DF2 <- structure(list(yr = c(1981L, 1981L, 1981L), mon = c(2L, 2L, 2L > ), d = 1:3, prod = c(17L, 20L, 21L), prod2 = c(49L, 47L, 52L), > prod3 = c(25L, 23L, 27L)), .Names = c("yr", "mon", "d", "prod", > "prod2", "prod3"), class = "data.frame", row.names = c(NA, -3L > )) > > Assuming they are the same structure as you said: > > colnames(DF2) <- colnames(DF1) > rbind(DF1, DF2) > # year month day product1 product2 product3 > # 1 1981 1 1 18 56 20 > # 2 1981 1 2 19 45 22 > # 3 1981 1 3 16 48 28 > # 4 1981 1 4 19 50 21 > # 5 1981 2 1 17 49 25 > # 6 1981 2 2 20 47 23 > # 7 1981 2 3 21 52 27 > > The attached .png image file shows you how to send plain text emails to > r-help using gmail. > > ------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of lily li > Sent: Wednesday, May 24, 2017 12:30 PM > To: R mailing list <r-help at r-project.org> > Subject: [R] about combining two dataframes > > Hi all, > > I have a question about combining two data frames. For example, there are > the two dataframes below, with the same structure but different column > names and column lengths. How to add the values in DF2 to the end of DF1, > though the column names do not match? How to add more than two? Thanks. > > DF1 > year month day product1 product2 product3 > 1981 1 1 18 56 20 > 1981 1 2 19 45 22 > 1981 1 3 16 48 28 > 1981 1 4 19 50 21 > > DF2 > yr mon d prod prod2 prod3 > 1981 2 1 17 49 25 > 1981 2 2 20 47 23 > 1981 2 3 21 52 27 > > I use the code below but it does not work. > require(dplyr) > bind_rows(DF1, DF2) or bind_cols(DF1, DF2) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]