[R] Converting Data Types
Martin Maechler
maechler at stat.math.ethz.ch
Wed May 21 09:31:20 CEST 2008
>>>>> "EW" == Edward Wijaya <ewijaya at gmail.com>
>>>>> on Wed, 21 May 2008 11:52:17 +0900 writes:
EW> Hi Peter,
EW> Thanks.
EW> as.matrix() does the trick.
and ... again my perennial remark to the above "trick" :
Do use data.matrix(dd) instead of
as.matrix(dd)
if dd is a data frame ... it will also produce a numeric matrix
when dd contains factor (and similar) columns.
Martin Maechler, ETH Zurich
EW> - Edward
EW> On Wed, May 21, 2008 at 11:31 AM, Peter Alspach
EW> <PAlspach at hortresearch.co.nz> wrote:
>> Edward
>>
>> Are you sure mymatrix is, in fact, a matrix and note a dataframe (which
>> is a list)? I get:
>>
>>> is.matrix(mymatrix)
>> [1] FALSE
>>> is.data.frame(mymatrix)
>> [1] TRUE
>>> samples <- mymatrix[1,]
>>> llgm <- dgamma(samples, scale=1, shape=2, log = TRUE)
>> Error in dgamma(x, shape, scale, log) :
>> Non-numeric argument to mathematical function
>>
>> That is, the same error as you when mymatrix is a dataframe. But
>> convert it to a matrix and:
>>
>>> mymatrix <- as.matrix(mymatrix)
>>> is.matrix(mymatrix)
>> [1] TRUE
>>> is.data.frame(mymatrix)
>> [1] FALSE
>>> samples <- mymatrix[1,]
>>> llgm <- dgamma(samples, scale=1, shape=2, log = TRUE)
>>> llgm
>> V1 V2 V3 V4 V5 V6
>> -99.25657 -84.01700 -237.40735 -201.26922 -170.53122 -159.29770
>>
>> HTH ....
>>
>> Peter Alspach
>>
>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org
>>> [mailto:r-help-bounces at r-project.org] On Behalf Of Edward Wijaya
>>> Sent: Wednesday, 21 May 2008 2:17 p.m.
>>> To: r-help at r-project.org
>>> Subject: [R] Converting Data Types
>>>
>>> Hi,
>>>
>>> How can I convert the matrices to list.
>>>
>>> For example I have this snippet:
>>>
>>> samples<-mymatrix[1,]
>>> print(samples)
>>>
>>> which prints:
>>>
>>> V1 V2 V3 V4 V5 V6
>>> 1 103.9 88.5 242.9 206.6 175.7 164.4
>>>
>>>
>>> How can I convert the object "samples" such that it prints:
>>> [1] 103.9 88.5 242.9 206.6 175.7 164.4
>>>
>>> The reason I ask this because I can't use the former
>>> "samples" object with this function:
>>>
>>> llgm <- dgamma(samples, scale=1, shape=2, log = TRUE)
>>>
>>> which gives this error:
>>> e 1374Error in dgamma(x, shape, scale, log) :
>>> Non-numeric argument to mathematical function
>>>
>>> Regards,
>>> Edward
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>> The contents of this e-mail are privileged and/or confidential to the named
>> recipient and are not to be used by any other person and/or organisation.
>> If you have received this e-mail in error, please notify the sender and delete
>> all material pertaining to this e-mail.
>>
EW> ______________________________________________
EW> R-help at r-project.org mailing list
EW> https://stat.ethz.ch/mailman/listinfo/r-help
EW> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
EW> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list