Peng Yu
2009-Sep-16 22:12 UTC
[R] How to convert a frame to a matrix while maintaining that a number is still a number?
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">
Henrique Dallazuanna
2009-Sep-16 22:17 UTC
[R] How to convert a frame to a matrix while maintaining that a number is still a number?
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
Maybe Matching Threads
- Modifying a data frame based on a vector that contains column numbers
- Problem setting the number of digits in xtable
- How should I denormalise a data frame list of lists column?
- cbind and cbind.data.frame
- [PATCH 09/23] nv50-: separate vertex formats from surface format descriptions