[R] merge problem

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Thu Sep 25 17:52:26 CEST 2003


Hi all,
   I just discovered a problem when merging two data.frames which don't 
have any columns in common and when one (or both) of the data.frames is 
a single column. E.g.

x <- data.frame(x = 1:5, y = letters[1:5])
y <- data.frame(z = 1:2)
z <- merge(x, y)

The result z is as expected, but the resulting names of z are not right. 
I've tracked it down to following the line in merge.data.frame:

if (l.b == 0) {
   ij <- expand.grid(1:nx, 1:ny)
   res <- cbind(x[ij[, 1], ], y[ij[, 2], ]) # <--- HERE
}

Changing this line to:

res <- cbind(x[ij[, 1], , drop = FALSE], y[ij[, 2], , drop = FALSE])

resolves the problem.

S-PLUS 6.1 does not have this problem.

Hope this is helpful to others.

-sundar




More information about the R-help mailing list