[R] How to convert character matrix or data.frame to numeric?
Ben Tupper
ben.bighair at gmail.com
Mon Jan 18 00:21:58 CET 2010
Hello again,
On Jan 17, 2010, at 5:42 PM, Rolf Turner wrote:
>
>
> Notice that your output is 6 x 5 whereas your input is 5 x 6.
> Now *what* did you do wrong?
>
Whoa! Let's try that again, but with a better example.
> X = matrix(as.character(1:6),2,3)
> X
[,1] [,2] [,3]
[1,] "1" "3" "5"
[2,] "2" "4" "6"
> apply(X,1,as.numeric)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
Ouch! Hmmm. From the "Value" section of the apply docs... "If each
call to FUN returns a vector of length n, then apply returns an array
of dimension c(n, dim(X)[MARGIN]) if n > 1." Since I set MARGIN to 1,
then I was operating on rows where n is 3.
> c(n, dim(X)[MARGIN])
[1] 3 2
How about that; it does just what it says it will do. I have no idea
why it does that, but the remedy seems to be ...
> apply(X, c(1,2), as.numeric)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
... or even ...
> apply(X, 2, as.numeric)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Perhaps someone could shed light on why the rows become columns.
Cheers,
Ben
> cheers,
>
> Rolf Turner
>
> On 18/01/2010, at 11:33 AM, Ben Tupper wrote:
>
>> Hi,
>>
>> I find the following works in R 2.10.1 on Mac OSX.
>>
>>> m = matrix(rep("3", 30), 5,6)
>>> m
>> [,1] [,2] [,3] [,4] [,5] [,6]
>> [1,] "3" "3" "3" "3" "3" "3"
>> [2,] "3" "3" "3" "3" "3" "3"
>> [3,] "3" "3" "3" "3" "3" "3"
>> [4,] "3" "3" "3" "3" "3" "3"
>> [5,] "3" "3" "3" "3" "3" "3"
>>> apply(m, 1,as.numeric)
>> [,1] [,2] [,3] [,4] [,5]
>> [1,] 3 3 3 3 3
>> [2,] 3 3 3 3 3
>> [3,] 3 3 3 3 3
>> [4,] 3 3 3 3 3
>> [5,] 3 3 3 3 3
>> [6,] 3 3 3 3 3
>>
>> Cheers,
>> Ben
>> On Jan 17, 2010, at 5:17 PM, 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 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.
>>
>>
>> Cheers,
>> Ben
>>
>> ______________________________________________
>> 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.
>
>
> ######################################################################
> Attention:This e-mail message is privileged and confidential. If you
> are not theintended recipient please delete the message and notify
> the sender.Any views or opinions presented are solely those of the
> author.
>
> This e-mail has been scanned and cleared by
> MailMarshalwww.marshalsoftware.com
> ######################################################################
Cheers,
Ben
More information about the R-help
mailing list