It will be difficult or impossible to store objects of class Date in a matrix -- you'll need to store them in a data frame:> pts = seq(as.Date("2011-01-01"), as.Date("2011-01-09"), by="1 day") > z = data.frame(pts[1:3],pts[4:6],pts[7:9]) > zpts.1.3. pts.4.6. pts.7.9. 1 2011-01-01 2011-01-04 2011-01-07 2 2011-01-02 2011-01-05 2011-01-08 3 2011-01-03 2011-01-06 2011-01-09> sapply(z,class)pts.1.3. pts.4.6. pts.7.9. "Date" "Date" "Date" - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Sat, 12 Mar 2011, Bogaso Christofer 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? > > > > Thanks > > > [[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. >
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="1day"), 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? Thanks [[alternative HTML version deleted]]
On Fri, Mar 11, 2011 at 4:15 PM, Bogaso Christofer <bogaso.christofer at 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
It is easy to recover the date by using as.Date:> 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> str(dat)num [1:3, 1:3] 14975 14976 14977 14978 14979 ...> as.Date(dat)[1] "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" "2011-01-07" "2011-01-08" [9] "2011-01-09">On Fri, Mar 11, 2011 at 4:15 PM, Bogaso Christofer <bogaso.christofer at 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? > > > > Thanks > > > ? ? ? ?[[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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?