[R] Annoyance in as.numeric

markleeds at verizon.net markleeds at verizon.net
Tue Jun 17 00:00:26 CEST 2008


  thanks for your correction Gavin. i read ?data.matrix and neglected to 
pay attention to the last line of the description:

"Factors and ordered factors are replaced by their internal codes".




On Mon, Jun 16, 2008 at  5:30 PM, Gavin Simpson wrote:

> On Mon, 2008-06-16 at 15:35 -0500, markleeds at verizon.net wrote:
>> hi: you can do below but i don't know if it's worth it ?
>>
>> newx <- data.matrix(data.frame(x))
>> print(newx)
>
> That doesn't work Mark:
>
>> str(data.frame(x))
> 'data.frame':   2 obs. of  2 variables:
>  $ X1: Factor w/ 2 levels "1","2": 1 2
>  $ X2: Factor w/ 2 levels "3","4": 1 2
>> data.matrix(data.frame(x))
>      X1 X2
> [1,]  1  1
> [2,]  2  2
>
> Which is as per documentation - the data.frame() coerces to factors 
> and
> data.matrix represents factor by their internal values.
>
> One "solution" might be to save the dims and then reapply:
>
>> mat
>      [,1] [,2]
> [1,] "1"  "3" [2,] "2"  "4"
>> dims <- dim(mat)
>> mat2 <- as.numeric(mat)
>> dim(mat) <- dims
>> mat2
>      [,1] [,2]
> [1,]    1    3
> [2,]    2    4
>
> You could wrap this in a function:
>
> asMatrix <- function(x, ...) {
>    dims <- dim(x)
>    x <- as.double(x)
>    dim(x) <- dims
>    return(x)
> }
>
> It would be logical to define a method for as.double for matrix 
> objects.
> However objects of class "matrix" are not objects in the sense of
> isObject() and hence method dispatch will not work in this case.
>
> HTH
>
> G
>
>
>>
>>
>> On Mon, Jun 16, 2008 at  4:22 PM, Alberto Monteiro wrote:
>>
>>> Why does as.numeric convert matrices and arrays to vectors?
>>>
>>> as.numeric(matrix(c("1", "2", "3", "4"), 2, 2))
>>> [1] 1 2 3 4
>>>
>>> I could only figure out ugly ways to bypass this, like:
>>>
>>> x <- matrix(c("1", "2", "3", "4"), 2, 2)
>>> array(as.numeric(x), dim = dim(x), dimnames = dimnames(x))
>>>
>>> Alberto Monteiro
>>>
>>> ______________________________________________
>>> 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.
>>
>> ______________________________________________
>> 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.
> -- 
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>  Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
>  ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
>  Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
>  Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
>  UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list