[R] as.double( factor(something) )??

Spencer Graves spencer.graves at pdf.com
Wed Nov 19 20:28:02 CET 2003


      Have you considered the following: 

      > f <- factor(1:2)
      > as.numeric(as.character(f))
      [1] 1 2

      See Venables and Ripley (2000) S Programming (Springer, p. 15). 

      Is this what you want?  spencer graves

RenE J.V. Bertin wrote:

>Hello,
>
>After converting a numeric variable into a factor, is there a way to convert it back to the original values? as.double() doesn't do that correctly, for evident reasons (I guess) and as shown below.
>
>Thanks,
>RenE Bertin
>
>  
>
>>dd<-c( rep(0,10), rep(1,10) )
>>dd
>>    
>>
> [1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
>  
>
>>as.double(dd)
>>    
>>
> [1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
>  
>
>>dd<-factor(dd)
>>dd
>>    
>>
> [1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
>Levels: 0 1
>  
>
>>as.double(dd)
>>    
>>
> [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
>  
>
>>dd<-c( rep(1,10), rep(0,10) )
>>as.double(dd)
>>    
>>
> [1] 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
>  
>
>>dd<-factor(dd)
>>dd
>>    
>>
> [1] 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
>Levels: 0 1
>  
>
>>as.double(dd)
>>    
>>
> [1] 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
>  
>
>>as.double(ordered(dd))
>>    
>>
> [1] 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
>  
>
>>dd<-c( rep(1,10), rep(0,10), rep(-2,10) )
>>dd
>>    
>>
> [1]  1  1  1  1  1  1  1  1  1  1  0  0  0  0  0  0  0  0  0  0 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2
>  
>
>>as.double(dd)
>>    
>>
> [1]  1  1  1  1  1  1  1  1  1  1  0  0  0  0  0  0  0  0  0  0 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2
>  
>
>>dd<-factor(dd)
>>dd
>>    
>>
> [1] 1  1  1  1  1  1  1  1  1  1  0  0  0  0  0  0  0  0  0  0  -2 -2 -2 -2 -2 -2 -2 -2 -2 -2
>Levels: -2 0 1
>  
>
>>as.double(dd)
>>    
>>
> [1] 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>  
>




More information about the R-help mailing list