[R] factor always have type integer

Liaw, Andy andy_liaw at merck.com
Wed Sep 8 22:00:10 CEST 2004

?data.frame says:


     A data frame is a list of variables of the same length with unique
     row names, given class '"data.frame"'.

     'data.frame' converts each of its arguments to a data frame by
     calling 'as.data.frame(optional=TRUE)'.  As that is a generic
     function, methods can be written to change the behaviour of
     arguments according to their classes: R comes with many such
     methods. Character variables passed to 'data.frame' are converted
     to factor columns unless protected by 'I'.  ...

(Note that last sentence.)  I believe that answers your question.


> From: Erich Neuwirth
> typeof applied to a factor always seems to return "integer",
> independently of the type of the levels.
> This has a strange side effect.
> When a variable is "imported" into a data frame,
> its type changes.
> character variables automatically are converted
> to factors when imported into data frames.
> Here is an example:
>  > v1<-1:3
>  > v2<-c("a","b","c")
>  > df<-data.frame(v1,v2)
>  > typeof(v2)
> [1] "character"
>  > typeof(df$v2)
> [1] "integer"
> It is somewhat surprising that
> the types of v2 and df$v2 are different.
> the answer is to do
> levels(df$v2)[df$v2]
> but that is somewhat involved.
> Should the types not be identical, and typeof applied to factors
> return the type of the levels?
> -- 
> Erich Neuwirth, Computer Supported Didactics Working Group
> Visit our SunSITE at http://sunsite.univie.ac.at
> Phone: +43-1-4277-38624 Fax: +43-1-4277-9386
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html

More information about the R-help mailing list