[R] factor always have type integer

Roger D. Peng rpeng at jhsph.edu
Wed Sep 8 22:11:51 CEST 2004


In some cases it makes sense to store "character" variables as factors 
(integers with labels) since this can take up much less memory.  If 
you really want to store `v2' as character, just do

data.frame(v1, I(v2))

-roger

Erich Neuwirth wrote:
> 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?
> 
>




More information about the R-help mailing list