[R] Impossible to merge with a zero rows data frame?

Bonfigli Sandro bonfigli at inmi.it
Wed Sep 27 11:55:44 CEST 2006


I'm trying to merge two data frames. One of them is a zero rows data 
frame.
I'm using the merge parameter 'all.x = TRUE' so I'd expect to obtain all
the rows of x. In fact the merge help says:

all.x: logical; if 'TRUE', then extra rows will be added to the
          output, one for each row in 'x' that has no matching row in
          'y'.  These rows will have 'NA's in those columns that are
          usually filled with values from 'y'.

To let you test the problem here is some code:

> L3 <- LETTERS[1:3]
> (d <- data.frame(cbind(x=1, y=1), fac=sample(L3, 1, repl=TRUE)))
> e <- d[-1,]

so now we have that:
> e
[1] x   y   fac
<0 rows> (or 0-length row.names)

here is the merge:
> merge(d, e, by.x = c("x"), by.y = c("x"), all.x = TRUE)

I'd expect something like:
  x y.x fac.x y.y fac.y
1 1   1     B  NA    NA
instead of:
Error in merge.data.frame(d, e, by.x = c("x"), by.y = c("x"), all.x = TRUE) : 
        no row to correspond
(I'm traslating the error message, so it could be a little different)

My questions are:
Is there a way to obtain my desired result? In the context in which I'd 
like to use the code the second data frame is the result of a query
and a lot of overwork would be added if I have to check if the dataframe
is a zero rows one BEFORE the merge (in fact I do a sequence of several
merge)
Is this behaviour of the merge command correct? Why did the developers
choose it?

P.S.: I tested this code both in R 2.3.0 and in 2.3.1

Thank you in advance.
   Sandro Bonfigli



More information about the R-help mailing list