[R] combining dataframes with different numbers of columns

Stephen D. Weigand weigand.stephen at charter.net
Wed Nov 8 03:51:44 CET 2006


Denis,

On Nov 7, 2006, at 8:30 PM, Denis Chabot 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.
>
> Here is a simple example. The initial dataframes are a and b. All
> variables in b are also in a.
>
> a <- data.frame(X=seq(1,10), matrix(runif(100, 0,15), ncol=10))
> b <- data.frame(X=seq(16,20), X4=runif(5,0,15))
>
> A merge does not work because the common variable X4 becomes 2
> variables, X4.x and X4.y.
>
> c <- merge(a,b,by="X", all=T)


[snipped]


> Thanks in advance,
>
> Denis Chabot
>

Will

   merge(a, b, by = intersect(names(a), names(b)), all = TRUE)

do what you want? (Note the 'by' argument uses the default so
it can be left out.)

Hope this helps,

Stephen

Stephen Weigand
Rochester, Minnesota, USA



More information about the R-help mailing list