From p.dalgaard@biostat.ku.dk  Tue Feb  8 10:09:53 2000
From: p.dalgaard@biostat.ku.dk (Peter Dalgaard BSA)
Date: 08 Feb 2000 11:09:53 +0100
Subject: [Rd] fix() changes character to factor (PR#415)
In-Reply-To: Andreas.Weingessel@ci.tuwien.ac.at's message of "Tue, 8 Feb 2000 10:48:11 +0100 (MET)"
References: <200002080948.KAA16514@pubhealth.ku.dk>
Message-ID: <x2u2jk9f9q.fsf@blueberry.kubism.ku.dk>

Andreas.Weingessel@ci.tuwien.ac.at writes:

> If x is a data.frame containing a column of mode character, this
> column is changed to a factor after applying fix(x).
> Example:
> 
> R> x_data.frame(a=1:3,b=letters[1:3],c=c("hansi","pepi","karli"))
> R> x$c_as.character(x$c)
> R> is.character(x$c)
> [1] TRUE
> R> is.factor(x$c)
> [1] FALSE
> R> fix(x)
> ## I change "karli" to "sepp"
> R> x$c
> [1] hansi pepi  sepp 
> Levels:  hansi pepi sepp 
> R>  is.character(x$c)
> [1] FALSE
> R> is.factor(x$c)
> [1] TRUE
> 
> Now, it is not possible to apply fix(x) again to undo your change,
> because edit does not allow to introduce new factor levels.

1) That is not the way to do that
2) The right way doesn't work either...

Character vectors in data frames will get converted to factors when
you least expect so, unless you protect them with the AsIs class,
i.e.  

x <- data.frame(a=1:3,b=letters[1:3],c=I(c("hansi","pepi","karli")))

Unfortunately, that will not work because is.vector is FALSE on x$c

The following fix to edit.data.frame seems to make things work:

    Is.vector <- function(x)is.vector(unclass(x))
    if (!all(sapply(name, Is.vector) | sapply(name, is.factor))) 
        stop("Can only handle vector and factor elements")

Anyone have a better idea?

(Of course the whole thing needs a redesign...)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] fix() changes character to factor (PR#415)
Peter Dalgaard BSA