Dear R users... I'd like to change this character vector, "zz", zz <- c("12","56","89") to the following numeric matrix. [,1] [,2] [1,] 1 2 [2,] 5 6 [3,] 8 9 Actually, "zz" vector has a long length. Any comments will be greatly appreciated. Kathryn Lord -- View this message in context: http://www.nabble.com/character-vector--%3E-numeric-matrix----tp24703927p24703927.html Sent from the R help mailing list archive at Nabble.com.
Hi, On Jul 28, 2009, at 1:13 PM, kathie wrote:> > Dear R users... > > I'd like to change this character vector, "zz", > > zz <- c("12","56","89") > > to the following numeric matrix. > > [,1] [,2] > [1,] 1 2 > [2,] 5 6 > [3,] 8 9 > > > Actually, "zz" vector has a long length. > > Any comments will be greatly appreciated. > > Kathryn Lordpieces <- strsplit(zz, "") # look at what pieces looks like my.data <- as.numeric(unlist(pieces)) my.matrix <- t(matrix(my.data, nrow=2)) my.matrix [,1] [,2] [1,] 1 2 [2,] 5 6 [3,] 8 9 -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Dear Kathie, Try also: t(sapply(zz, function(x) as.numeric( strsplit(x, "")[[1]] ))) If you want to delete the column names, then res <- t(sapply(zz, function(x) as.numeric( strsplit(x, "")[[1]] ))) dimnames(res) <- NULL res should do it. HTH, Jorge On Tue, Jul 28, 2009 at 1:13 PM, kathie <kathryn.lord2000@gmail.com> wrote:> > Dear R users... > > I'd like to change this character vector, "zz", > > zz <- c("12","56","89") > > to the following numeric matrix. > > [,1] [,2] > [1,] 1 2 > [2,] 5 6 > [3,] 8 9 > > > Actually, "zz" vector has a long length. > > Any comments will be greatly appreciated. > > Kathryn Lord > > -- > View this message in context: > http://www.nabble.com/character-vector--%3E-numeric-matrix----tp24703927p24703927.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
If your entire dataset looks like your example, try: do.call(rbind, sapply(zz, strsplit, split="")) Note that strsplit() requires a character vector as its first parameter. -Brian J. Koch Data Manager Decision Development Inc www.decisiondevelopment.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of kathie Sent: Tuesday, July 28, 2009 12:14 PM To: r-help at r-project.org Subject: [R] character vector -> numeric matrix ?? Dear R users... I'd like to change this character vector, "zz", zz <- c("12","56","89") to the following numeric matrix. [,1] [,2] [1,] 1 2 [2,] 5 6 [3,] 8 9 Actually, "zz" vector has a long length. Any comments will be greatly appreciated. Kathryn Lord -- View this message in context: http://www.nabble.com/character-vector--%3E-numeric-matrix----tp24703927 p24703927.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.