[R] Re: [S] Problem with Merge

Rick rab at nauticom.net
Fri Feb 8 16:30:50 CET 2002


Don,

I had meant this for r-help, but it probably is the same in S.

The last time I tried to use rbind in R, it screwed up the data frame if
the data frame contained dates or character vectors. Apparently that has
been changed. Although there is still no reason why merge should not work
in the case where all vectors are in common. It's an uncessary restriction
which isn't even mentioned in the function information as far as I can
see.

Rick Bilonick

Don MacQueen wrote:

> A side-by-side, or relational, merge of two data frames with exactly
> the same set of variables doesn't make any sense. Perhaps you wanted
> an end-to-end concatenation of them, that is, have a new data frame
> with the same variables and all the rows from both. For that, use
> rbind().
> In your example, rbind(xa[,1:2],xb[,1:2])
>
> -Don
>
> >I want to merge two data frames that have the same set of variable
> >names. The docs on merge don't say this isn't possible but it
> >doesn't appear to be possible.
> >
> >>  xa
> >   a  b c1
> >1 1 10  1
> >2 2 11  2
> >3 3 12  3
> >>  xb
> >   a  b c2
> >1 3 12  1
> >2 4 14  2
> >3 5 15  3
> >4 6 16  5
> >>  merge(xa,xb,all=T)
> >   a  b c1 c2
> >1 1 10  1 NA
> >2 2 11  2 NA
> >3 3 12  3  1
> >4 4 14 NA  2
> >5 5 15 NA  3
> >6 6 16 NA  5
> >>  merge(xa[,1:2],xb[,1:2],all=T)  # Only the identical vector names
> >Error in "names<-.default"(*tmp*, value = vnames) :
> >  names attribute must be the same length as the vector
> >>merge(xa,xb[,1:2],all=T) # Only one data frame has a nonmatching name
> >Error: attempt to select less than one element
> >>
> >
> >It appears that for merge to work, there must be at least one
> >variable in each data frame that does not match. So in order to
> >merge two data frames with the same set of
> >names (something that would seem to be frequently needed), you have
> >to add one dummy vector with different names to each data frame.
> >
> >The docs for merge say that if you don't specify a "by" variable,
> >all variables in common are used. I don't see it mentioned that you
> >can't have all variables in
> >common.
> >
> >Rick Bilonick
> >
> >--------------------------------------------------------------------
> >This message was distributed by s-news at lists.biostat.wustl.edu.  To
  .................
  .................
>
> --
> --------------------------------------
> Don MacQueen
> Environmental Protection Department
> Lawrence Livermore National Laboratory
> Livermore, CA, USA
> --------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list