Andreas.Weingessel@ci.tuwien.ac.at
2000-Feb-08 09:48 UTC
[Rd] fix() changes character to factor (PR#415)
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. --please do not edit the information below-- Version: platform = i686-unknown-linux arch = i686 os = linux system = i686, linux status = major = 0 minor = 99.0 year = 2000 month = February day = 7 language = R Search Path: .GlobalEnv, Autoloads, package:base -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._