[R] merge() generates a factor: bug?

David Brahm brahm at alum.mit.edu
Mon Jan 28 19:52:25 CET 2002


Hello,

   When I merge two dataframes, each created by read.table and containing no
factors, the result has columns which have been converted to factor.  I think
this is a bug (and may be related to some fixed bugs, eg PR#1102, PR#1121), but
will await comments before submitting.  Also, I'm not sure if the bug is in
merge() or in read.table().  I am using R-1.4.0 patch 1/13/02 on Solaris 2.6.

File "merge1.txt":
k a
A w
B x

File "merge2.txt":
k b
A y
C z

Bug-testing code:
R> x <- read.table("merge1.txt",header=T,row.names=NULL,colClasses="character")
R> y <- read.table("merge2.txt",header=T,row.names=NULL,colClasses="character")
R> z <- merge(x, y, all.x=T)
R> sapply(z, is.factor)
    k     a     b 
FALSE FALSE  TRUE 

Note z$b has become a factor!  I strongly suspect it has to do with setting
all.x=T and the fact that some rows of z$b (for key k="B") had to be filled in.

Also note, I do not get this behavior if I create "x" and "y" with data.frame:
R> x <- data.frame(k=I(c("A","B")), a=I(c("w","x")))
R> y <- data.frame(k=I(c("A","C")), b=I(c("y","z")))
R> z <- merge(x, y, all.x=T)
R> sapply(z, is.factor)
    k     a     b 
FALSE FALSE FALSE 


Version:
 platform = sparc-sun-solaris2.6
 arch = sparc
 os = solaris2.6
 system = sparc, solaris2.6
 status = Patched
 major = 1
 minor = 4.0
 year = 2002
 month = 01
 day = 13
 language = R

Search Path:
 .GlobalEnv, package:secref, package:misc, package:io, package:arrays, package:ls1, package:g.data, package:db, package:ts, package:ctest, Autoloads, package:base

-- 
                              -- David Brahm (brahm at alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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