[R] Assignment converts variable to factor
David Kane <David Kane
a296180 at agate.fmr.com
Wed Jul 10 13:31:21 CEST 2002
Daniel Mastropietro writes:
> Hello,
>
> I would like to know if this behaviour in R is as expected.
It is as expected in R 1.5.1. Note that the NEWS for R-devel (1.6.0 to be)
says:
o [[<-.data.frame no longer coerces character replacement values
to factor. This is consistent with using $ to replace and
with S4.
So, as best that I understand the details, the behavior will be changing. There
was some discussion of this topic on r-help and r-devel a month or two ago
(consisting mostly of my confusion and Brian Ripley's patient explanations).
See:
http://www.r-project.org/nocvs/mail/r-help/2002/4016.html
Or the thread beginning with:
http://www.r-project.org/nocvs/mail/r-devel/2002/0777.html
Regards,
Dave Kane
> I have a data frame 'dat' with column var1 being character (and not
> factor). Then I create a new column 'var2' by:
>
> > dat[,"var2"] <- dat$var1;
>
> Column var2 is now a factor.
>
> But if I do:
>
> > dat$var2 <- dat$var1;
>
> Then column var2 is character (and not factor).
>
> I don't want to have var2 as factor by doing the first assignment. I tried
> using 'as.character(dat$var1)' or converting dat[,"var2"] to character by
> using as.character() after the assignment, but still var2 is treated as factor.
>
> Note that I want to use dat[,"var2"] and not dat$var2 because I am doing
> the assignment within a function by referencing column var2 with a variable
> whose value is "var2" (e.g. dat[,col2] <- dat$var1, where col2 = "var2").
> It is easier and more clear to use the [] operator to reference a data
> frame column with a variable, rather than using the $ operator with the
> parse() function.
>
> Thanks
> Daniel Mastropietro
>
>
> - -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
--
David Kane
Geode Capital Management
617-563-0122
david.d.kane at fmr.com
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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