[R] Convert matrix to numeric

David Winsemius dwinsemius at comcast.net
Wed Aug 3 22:28:09 CEST 2011


Here's what you _should_  do

1) transpose
2a) as.data.frame
3a) fix the stupid default stringsAsFactor behavior
4a) convert the first 5 columns to numeric

dfrm <- as.data.frame( t( structure(.) ) )
dfrm[, 1:5] <-lapply(dfrm[, 1:5], as.character)
dfrm[, 1:5] <-lapply(dfrm[, 1:5], as.numeric)

Or:
1) transpose
2b) as.data.frame with stringsAsFactors= FALSE
3b) convert to numeric




On Aug 3, 2011, at 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.
>
>
>
> I also tried converting this to a table then converting to numeric,  
> but I got: (list) object cannot be coerced to type 'double'
>
>
>
> Jeff 		 	   		
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list