# Convert matrix to numeric

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Convert matrix to numeric

 I have a matrix that looks like this: structure(c("0.0376673981759913", "0.111066500741386", "1", "1103", "18", "OPEN", "DEPR", "0.0404073656092023", "0.115186044704599", "1", "719", "18", "OPEN", "DEPR", "0.0665342096693433", "0.197570061769498", "1", "1103", "18", "OPEN", "DEPR", "0.119287147905722", "0.356427096010845", "1", "1103", "18", "OPEN", "DEPR"), .Dim = c(7L, 4L), .Dimnames = list(     c("Sn", "SlnC", "housenum", "date", "hour", "flue", "pressurization"     ), c("10019.BLO", "1002.BLO", "10020.BLO", "10021.BLO")))   How do I convert rows 1-5 to numeric?  I tried mode() <- "numeric" but that doesn't change anything.   I also tried converting this to a table then converting to numeric, but I got: (list) object cannot be coerced to type 'double'   Jeff     ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Convert matrix to numeric

 On 03/08/2011 3:04 PM, Jeffrey Joh wrote: > I have a matrix that looks like this: > > > structure(c("0.0376673981759913", "0.111066500741386", "1", "1103", > "18", "OPEN", "DEPR", "0.0404073656092023", "0.115186044704599", > "1", "719", "18", "OPEN", "DEPR", "0.0665342096693433", "0.197570061769498", > "1", "1103", "18", "OPEN", "DEPR", "0.119287147905722", "0.356427096010845", > "1", "1103", "18", "OPEN", "DEPR"), .Dim = c(7L, 4L), .Dimnames = list( >      c("Sn", "SlnC", "housenum", "date", "hour", "flue", "pressurization" >      ), c("10019.BLO", "1002.BLO", "10020.BLO", "10021.BLO"))) > > > > How do I convert rows 1-5 to numeric?  I tried mode()<- "numeric" but that doesn't change anything. Every entry in a matrix has the same type, so you can't change just those rows other than by extracting them into a separate matrix and changing that. Duncan Murdoch > > > I also tried converting this to a table then converting to numeric, but I got: (list) object cannot be coerced to type 'double' > > > > Jeff   > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Convert matrix to numeric

 In reply to this post by Jeffrey Joh How about Matrix[1:5,]=as.numeric(Matrix[1:5,]) -Ken Hutchison On Aug 3, 2554 BE, at 3:04 PM, Jeffrey Joh <[hidden email]> wrote: > > I have a matrix that looks like this: > > > structure(c("0.0376673981759913", "0.111066500741386", "1", "1103", > "18", "OPEN", "DEPR", "0.0404073656092023", "0.115186044704599", > "1", "719", "18", "OPEN", "DEPR", "0.0665342096693433", "0.197570061769498", > "1", "1103", "18", "OPEN", "DEPR", "0.119287147905722", "0.356427096010845", > "1", "1103", "18", "OPEN", "DEPR"), .Dim = c(7L, 4L), .Dimnames = list( >    c("Sn", "SlnC", "housenum", "date", "hour", "flue", "pressurization" >    ), c("10019.BLO", "1002.BLO", "10020.BLO", "10021.BLO"))) > > > > How do I convert rows 1-5 to numeric?  I tried mode() <- "numeric" but that doesn't change anything. > > > > I also tried converting this to a table then converting to numeric, but I got: (list) object cannot be coerced to type 'double' > > > > Jeff                         > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Convert matrix to numeric

 In reply to this post by Jeffrey Joh Hi Jeffrey, On Wed, Aug 3, 2011 at 3:04 PM, Jeffrey Joh <[hidden email]> wrote: > > I have a matrix that looks like this: > > > structure(c("0.0376673981759913", "0.111066500741386", "1", "1103", > "18", "OPEN", "DEPR", "0.0404073656092023", "0.115186044704599", > "1", "719", "18", "OPEN", "DEPR", "0.0665342096693433", "0.197570061769498", > "1", "1103", "18", "OPEN", "DEPR", "0.119287147905722", "0.356427096010845", > "1", "1103", "18", "OPEN", "DEPR"), .Dim = c(7L, 4L), .Dimnames = list( >    c("Sn", "SlnC", "housenum", "date", "hour", "flue", "pressurization" >    ), c("10019.BLO", "1002.BLO", "10020.BLO", "10021.BLO"))) Thank you for providing a small working example. > How do I convert rows 1-5 to numeric?  I tried mode() <- "numeric" but that doesn't change anything. Two things are going on here. First, a matrix can only contain one kind of data. For this example, since there are strings the whole thing has to be character. A data frame is intended to hold different kinds of data, but each column has to be a single type. So if you want those values to be numeric instead of character, you'll need to transpose your matrix and convert it to a data frame. tempdata <- structure(c("0.0376673981759913", "0.111066500741386", "1", "1103", "18", "OPEN", "DEPR", "0.0404073656092023", "0.115186044704599", "1", "719", "18", "OPEN", "DEPR", "0.0665342096693433", "0.197570061769498", "1", "1103", "18", "OPEN", "DEPR", "0.119287147905722", "0.356427096010845", "1", "1103", "18", "OPEN", "DEPR"), .Dim = c(7L, 4L), .Dimnames = list(    c("Sn", "SlnC", "housenum", "date", "hour", "flue", "pressurization"    ), c("10019.BLO", "1002.BLO", "10020.BLO", "10021.BLO"))) tempdata <- data.frame(t(tempdata), stringsAsFactors=FALSE) Once you have the right kind of object, you can convert the five columns of interest to numeric. This needs to be done a column at a time, I think: tempdata[, 1:5] <- apply(tempdata[,1:5], 2, as.numeric) Sarah -- Sarah Goslee http://www.functionaldiversity.org______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Convert matrix to numeric

 In reply to this post by Ken Hutchison > -----Original Message----- > From: [hidden email] [mailto:r-help-bounces@r- > project.org] On Behalf Of Ken > Sent: Wednesday, August 03, 2011 12:13 PM > To: Jeffrey Joh > Cc: <[hidden email]> > Subject: Re: [R] Convert matrix to numeric > > How about > Matrix[1:5,]=as.numeric(Matrix[1:5,]) > -Ken Hutchison > > On Aug 3, 2554 BE, at 3:04 PM, Jeffrey Joh <[hidden email]> > wrote: > > > > > I have a matrix that looks like this: > > > > > > structure(c("0.0376673981759913", "0.111066500741386", "1", "1103", > > "18", "OPEN", "DEPR", "0.0404073656092023", "0.115186044704599", > > "1", "719", "18", "OPEN", "DEPR", "0.0665342096693433", > "0.197570061769498", > > "1", "1103", "18", "OPEN", "DEPR", "0.119287147905722", > "0.356427096010845", > > "1", "1103", "18", "OPEN", "DEPR"), .Dim = c(7L, 4L), .Dimnames = > list( > >    c("Sn", "SlnC", "housenum", "date", "hour", "flue", > "pressurization" > >    ), c("10019.BLO", "1002.BLO", "10020.BLO", "10021.BLO"))) > > > > > > > > How do I convert rows 1-5 to numeric?  I tried mode() <- "numeric" > but that doesn't change anything. > > Ken, You can't store the numeric values back in the matrix, because rows 6 and 7 contain character values.  Everything will just be converted back to character.  You need to create a new matrix for the numeric values. Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.