On Mar 13, 2011, at 11:46 AM, Bogaso Christofer wrote:

Thanks everyone for clarifying my query. However I was wondering why that Date character is not preserved within a matrix? Why R forcefully changes that to numeric? I am especially concerned because as per the definition of
the matrix, each element needs to be of same class.

THe help page for `matrix` says that there is coercion with `as.vector` and the help page for `as.vector` describes further specific defaults for that operation, so I think your mental model of what matrix requires may need refinement from a simplistic notion of "matrix needs same class".

My reading could be wrong but it appears any vector of a non-atomic class is going to be coerced to an atomic class (as well as having any names stripped off).

--
David.

Is there any other
example other than Date class which R forcefully changes to some simpler
class if it placed within a matrix?

Thanks and regards,


-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com]
Sent: 12 March 2011 02:46
To: Bogaso Christofer
Cc: r-help@r-project.org
Subject: Re: [R] 'Date' elements within a matrix

On Fri, Mar 11, 2011 at 4:15 PM, Bogaso Christofer
<bogaso.christo...@gmail.com> wrote:
Dear all, when I put date objects (class of 'Date') in a matrix it
becomes
numeric:

dat <- matrix(seq(as.Date("2011-01-01"), as.Date("2011-01-09"), by="1
day"), 3)

dat

     [,1]  [,2]  [,3]

[1,] 14975 14978 14981

[2,] 14976 14979 14982

[3,] 14977 14980 14983

class(dat[1,1])

[1] "numeric"



As it could not preserve the 'Date' characteristics after putting my
Date observations, I find it difficult to carry forward further
calculation with Dates. Can somebody help me on how to preserve the
Data properties while storing them within some matrix?

If its important to you to store them in a matrix (as opposed to a data
frame) then try storing them in a matrix of lists:

dd <- seq(as.Date("2011-01-01"), as.Date("2011-01-09"), by = "day") m
<- matrix(as.list(dd), 3) m[[1,1]]
[1] "2011-01-01"


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
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.

David Winsemius, MD
West Hartford, CT

______________________________________________
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.

Reply via email to