[R] How to convert a frame to a matrix while maintaining that a number is still a number?

Henrique Dallazuanna wwwhsd at gmail.com
Thu Sep 17 00:17:16 CEST 2009


You can't do this using matrix class,

matrix convert all values to same class, in this case, character.

On Wed, Sep 16, 2009 at 7:12 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
> Hi,
>
> See the code below.
>
> 'x' is a frame. x$C1 are all numbers 1, 2 and 3.
>
> 'as.matrix(x)' convert x$C1 to strings "1", "2" and "3". I'm wondering
> how to maintain that the first column of 'as.matrix(x)' still numbers.
>
> Regards,
> Peng
>
> $ cat read.csv
> "C1","C2"
> "1","x"
> "2","y"
> "3","z"
> $ Rscript read.csv.R
>> x=read.csv("read.csv")
>> str(x)
> 'data.frame':   3 obs. of  2 variables:
>  $ C1: int  1 2 3
>  $ C2: Factor w/ 3 levels "x","y","z": 1 2 3
>> as.matrix(x)
>     C1  C2
> [1,] "1" "x"
> [2,] "2" "y"
> [3,] "3" "z"
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list