Peng Yu
2009-Sep-16 22:33 UTC
[R] How not to keep the redundant space when convert a data frame with as.matrix()?
Hi, The first column in as.matrix(x) has extra spaces (" "), which I don't want. Is there a way not to generate those spaces? Regards, Peng> x=data.frame(x=1:10,y=letters[11:20]) > as.matrix(x)x y [1,] " 1" "k" [2,] " 2" "l" [3,] " 3" "m" [4,] " 4" "n" [5,] " 5" "o" [6,] " 6" "p" [7,] " 7" "q" [8,] " 8" "r" [9,] " 9" "s" [10,] "10" "t"
Daniel Nordlund
2009-Sep-16 22:41 UTC
[R] How not to keep the redundant space when convert a data frame with as.matrix()?
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Peng Yu > Sent: Wednesday, September 16, 2009 3:33 PM > To: r-help at stat.math.ethz.ch > Subject: [R] How not to keep the redundant space when convert > a data frame with as.matrix()? > > Hi, > > The first column in as.matrix(x) has extra spaces (" "), which I don't > want. Is there a way not to generate those spaces? > > Regards, > Peng > > > x=data.frame(x=1:10,y=letters[11:20]) > > as.matrix(x) > x y > [1,] " 1" "k" > [2,] " 2" "l" > [3,] " 3" "m" > [4,] " 4" "n" > [5,] " 5" "o" > [6,] " 6" "p" > [7,] " 7" "q" > [8,] " 8" "r" > [9,] " 9" "s" > [10,] "10" "t" >You have asked several questions about using as.matrix() on a data.frame with colums of different types, in particular character and numeric types. Since matrices can only have one type, your numerics are coerced to character. Is this what you really want? Is there some reason you can't use a data.frame? Enquiring minds want to know? Dan Daniel Nordlund Bothell, WA USA
Don McKenzie
2009-Sep-16 22:41 UTC
[R] How not to keep the redundant space when convert a data frame with as.matrix()?
If you want a matrix of strings > x <- data.frame(x=as.character(1:10),y=letters[11:20]) > as.matrix(x) x y [1,] "1" "k" [2,] "2" "l" [3,] "3" "m" [4,] "4" "n" [5,] "5" "o" [6,] "6" "p" [7,] "7" "q" [8,] "8" "r" [9,] "9" "s" [10,] "10" "t" BTW I believe the "=" for assignments is discouraged. Use "<-".>> x=data.frame(x=as.character(1:10),y=letters[11:20]) >> as.matrix(x)On 16-Sep-09, at 3:33 PM, Peng Yu wrote:> Hi, > > The first column in as.matrix(x) has extra spaces (" "), which I don't > want. Is there a way not to generate those spaces? > > Regards, > Peng > >> x=data.frame(x=1:10,y=letters[11:20]) >> as.matrix(x) > x y > [1,] " 1" "k" > [2,] " 2" "l" > [3,] " 3" "m" > [4,] " 4" "n" > [5,] " 5" "o" > [6,] " 6" "p" > [7,] " 7" "q" > [8,] " 8" "r" > [9,] " 9" "s" > [10,] "10" "t" > > ______________________________________________ > 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.Don McKenzie, Research Ecologist Pacific WIldland Fire Sciences Lab US Forest Service Affiliate Professor School of Forest Resources, College of the Environment CSES Climate Impacts Group University of Washington desk: 206-732-7824 cell: 206-321-5966 dmck at u.washington.edu donaldmckenzie at fs.fed.us