Hello R experts, can you tell me a simple way to convert a charactor matrix to numeric matrix? if I use as.numeric, the matrix is converted to a vector. a<-cbind(c("23","54","65"),c("1","2","3")) a [,1] [,2] [1,] "23" "1" [2,] "54" "2" [3,] "65" "3" as.numeric(a) [1] 23 54 65 1 2 3 thanks Jian [[alternative HTML version deleted]]
try this:> a<-cbind(c("23","54","65"),c("1","2","3")) > a[,1] [,2] [1,] "23" "1" [2,] "54" "2" [3,] "65" "3"> mode(a) <- 'numeric' > a[,1] [,2] [1,] 23 1 [2,] 54 2 [3,] 65 3>On Wed, Jun 2, 2010 at 8:15 AM, Yuan Jian <jayuan2008 at yahoo.com> wrote:> Hello R experts, > can you tell me a simple way to convert a charactor matrix to numeric matrix? > if I use as.numeric, the matrix is converted to a vector. > > a<-cbind(c("23","54","65"),c("1","2","3")) > a > ???? [,1] [,2] > [1,] "23" "1" > [2,] "54" "2" > [3,] "65" "3" > > ?as.numeric(a) > [1] 23 54 65? 1? 2? 3 > > thanks > Jian > > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hi you have several options apply(a, 2, as.numeric) matrix(as.numeric(a),3,2) b<-as.numeric(a) dim(b)<-c(3,2) Regards Petr r-help-bounces at r-project.org napsal dne 02.06.2010 14:15:25:> Hello R experts, > can you tell me a simple way to convert a charactor matrix to numericmatrix?> if I use as.numeric, the matrix is converted to a vector. > > a<-cbind(c("23","54","65"),c("1","2","3")) > a > [,1] [,2] > [1,] "23" "1" > [2,] "54" "2" > [3,] "65" "3" > > as.numeric(a) > [1] 23 54 65 1 2 3 > > thanks > Jian > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi Jian, Try matrix(as.numeric(a), ncol = 2) HTH, Jorge On Wed, Jun 2, 2010 at 8:15 AM, Yuan Jian <> wrote:> Hello R experts, > can you tell me a simple way to convert a charactor matrix to numeric > matrix? > if I use as.numeric, the matrix is converted to a vector. > > a<-cbind(c("23","54","65"),c("1","2","3")) > a > [,1] [,2] > [1,] "23" "1" > [2,] "54" "2" > [3,] "65" "3" > > as.numeric(a) > [1] 23 54 65 1 2 3 > > thanks > Jian > > > > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]