In respect of your matrix problem:
(1) mode(mm) <- "numeric"
OR:
(2) mm <- apply(mm,2,as.numeric)
In respect of your data frame problem:
as.data.frame(lapply(X,as.numeric))
(where X is your data frame) should work. This will of course
convert every column of your data frame to numeric mode. If there
are some columns that you wish to leave alone you will have to do
something a little bit more subtle. But not much more subtle.
It's all pretty easy if you learn some basic R syntax.
cheers,
Rolf Turner
On 18/01/2010, at 11:17 AM, ivan popivanov wrote:
Hello,
This turned out to be surprisingly hard for me:
Let's say I have
mm = matrix(as.character(seq(1,30, 1)), nrow=3); mm
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "1" "4" "7" "10" "13" "16" "19" "22" "25" "28"
[2,] "2" "5" "8" "11" "14" "17" "20" "23" "26" "29"
[3,] "3" "6" "9" "12" "15" "18" "21" "24" "27" "30"
which is a matrix of strings, I'd like to convert this to a matrix
of the corresponding numbers:
nn = matrix(seq(1,30, 1), nrow=3), nn
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 4 7 10 13 16 19 22 25 28
[2,] 2 5 8 11 14 17 20 23 26 29
[3,] 3 6 9 12 15 18 21 24 27 30
I can convert each dimension using sapply(mm[,1], as.numeric), but
how to convert the full matrix?!
In fact I hit this problem because I got a data.frame consisting of
factors (numerics represented as strings) when I queried a
database. So if you can advise how to convert this data.frame to a
data.frame with proper numerics - that would be even better. The
problem is that my numerics are doubles and the data.matrix
function seems to cast factors to ints.
Thanks in advance,
Ivan
_________________________________________________________________
[[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.
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
______________________________________________
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.