[R] combining dataframes with different numbers of columns

hadley wickham h.wickham at gmail.com
Wed Nov 8 03:46:45 CET 2006


On 11/7/06, Denis Chabot <chabotd at globetrotter.net> wrote:
> Dear list members,
>
> I have to combine dataframes together. However they contain different
> numbers of variables. It is possible that all the variables in the
> dataframe with fewer variables are contained in the dataframe with
> more variables, though it is not always the case.
>
> There are key variables identifying observations. These could be used
> in a merge statement, although this won't quite work for me (see below).
>
> I was hoping to find a way to combine dataframes where I needed only
> to ensure the key variables were present. The total number of
> variables in the final dataframe would be the total number of
> different variables in both initial dataframes. Variables that were
> absent in one dataframe would automatically get missing values in the
> joint dataframe.

Have a look at rbind.fill in the reshape package.

library(reshape)
rbind.fill(data.frame(a=1), data.frame(b=2))
rbind.fill(data.frame(a=1), data.frame(a=2, b=2))


Hadley



More information about the R-help mailing list