[R] merging data frames with matrix objects when missing cases

Kari Ruohonen kari.ruohonen at utu.fi
Fri Sep 18 09:18:55 CEST 2009


Hi,
I have faced a problem with the merge() function when trying to merge
two data frames that have a common index but the second one does not
have cases for all indexes in the first one. With usual variables R
fills in the missing cases with NA if all=T is requested. But if the
variable is a matrix R seems to insert NA only to the first column of
the matrix and fill in the rest of the columns by recycling the values.
Here is a toy example:

> df1<-data.frame(a=1:3,X1=I(matrix(1:6,ncol=2)))
> df2<-data.frame(a=1:2,X2=I(matrix(11:14,ncol=2)))
> merge(df1,df2)
  a X1.1 X1.2 X2.1 X2.2
1 1    1    4   11   13
2 2    2    5   12   14  
# no all=T, missing cases are dropped

> merge(df1,df2,all=T)
  a X1.1 X1.2 X2.1 X2.2
1 1    1    4   11   13
2 2    2    5   12   14
3 3    3    6   NA   13 
# X2.1 set to NA correctly but X2.2 set to 13 by recycling.

Can I somehow get the behaviour that the third row of the second matrix
X2 in the above example would be filled with NA for all columns? None of
the merge() options does not seem to provide a solution.

regards, Kari




More information about the R-help mailing list