Bonfigli Sandro
2006-Sep-27 09:55 UTC
[R] Impossible to merge with a zero rows data frame?
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
Hi
you have two options:
change the source code for merge
or use some modified function like
my.merge <- function(x,y, ...) if(all(dim(x)[1]>0, dim(y)[1]>0))
merge(x,y) else print ("No merge or whatever action which is
suitable")
HTH
Petr
On 27 Sep 2006 at 11:55, Bonfigli Sandro wrote:
Date sent: Wed, 27 Sep 2006 11:55:44 +0200
From: "Bonfigli Sandro" <bonfigli at inmi.it>
To: r-help at stat.math.ethz.ch
Subject: [R] Impossible to merge with a zero rows data frame?
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented,
> minimal, self-contained, reproducible code.
Petr Pikal
petr.pikal at precheza.cz