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:[email protected]] On Behalf Of lily li
Sent: Wednesday, May 24, 2017 12:30 PM
To: R mailing list <[email protected]>
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]]
______________________________________________
[email protected] 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.
______________________________________________
[email protected] 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.