[R] about combining two dataframes

lily li chocold12 at gmail.com
Wed May 24 19:46:09 CEST 2017


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]]



More information about the R-help mailing list