I have a column in a data frame that has a class of "Date" and a mode of "numeric". When I: max(df$Date) My output stays in Date format, i.e. "2006-01-03". However, when I run the following statment: tapply(df$Date, df$SomeFactor, max) my output looks like this: 9129 9493 9861 10226 10591 10956 11320 11687 12052 12417 The returned object is of mode "numeric" and class "array". Each array element is of mode "numeric" and class "numeric". I believe that this is the integer representation of my date. I can't seem to convert it back to a date. How do I get these to be intrepreted as a date instead of a number? Thanks, ~Dave R.
Try following this example:> foo <- Sys.time() > class(foo)[1] "POSIXt" "POSIXct"> ick <- as.numeric(foo) > ick[1] 1138667199> class(ick)[1] "numeric"> class(ick) <- class(foo) > ick[1] "2006-01-30 16:26:39 PST" -Don At 3:51 PM -0500 1/30/06, David Randel wrote:>I have a column in a data frame that has a class of "Date" and a mode of >"numeric". When I: > >max(df$Date) > >My output stays in Date format, i.e. "2006-01-03". > >However, when I run the following statment: > >tapply(df$Date, df$SomeFactor, max) > >my output looks like this: 9129 9493 9861 10226 10591 10956 11320 >11687 12052 12417 > >The returned object is of mode "numeric" and class "array". Each array >element is of mode "numeric" and class "numeric". I believe that this is >the integer representation of my date. I can't seem to convert it back to a >date. > >How do I get these to be intrepreted as a date instead of a number? > >Thanks, >~Dave R. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
Using this test data, say: dd <- Sys.Date() + 0:9 fac <- gl(5,2) Try this: do.call("c", tapply(dd, fac, max, simplify = FALSE)) or if a list result is ok then just tapply(dd, fac, max, simplify = FALSE) Another possibility using the as.Date.numeric method in the zoo package is: library(zoo) as.Date(tapply(dd, fac, max)) For more info on the internal representation of "Date" class objects see the Help desk article in R News 4/1. On 1/30/06, David Randel <davidr00 at hotmail.com> wrote:> I have a column in a data frame that has a class of "Date" and a mode of > "numeric". When I: > > max(df$Date) > > My output stays in Date format, i.e. "2006-01-03". > > However, when I run the following statment: > > tapply(df$Date, df$SomeFactor, max) > > my output looks like this: 9129 9493 9861 10226 10591 10956 11320 > 11687 12052 12417 > > The returned object is of mode "numeric" and class "array". Each array > element is of mode "numeric" and class "numeric". I believe that this is > the integer representation of my date. I can't seem to convert it back to a > date. > > How do I get these to be intrepreted as a date instead of a number? > > Thanks, > ~Dave R. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
tapply(df$Date, df$SomeFactor, max, simplify=FALSE) works, i.e. it is the unlist() which is losing the class (and perhaps unlist or tapply should be a bit cleverer). On Mon, 30 Jan 2006, David Randel wrote:> I have a column in a data frame that has a class of "Date" and a mode of > "numeric". When I: > > max(df$Date) > > My output stays in Date format, i.e. "2006-01-03". > > However, when I run the following statment: > > tapply(df$Date, df$SomeFactor, max) > > my output looks like this: 9129 9493 9861 10226 10591 10956 11320 > 11687 12052 12417 > > The returned object is of mode "numeric" and class "array". Each array > element is of mode "numeric" and class "numeric". I believe that this is > the integer representation of my date. I can't seem to convert it back to a > date. > > How do I get these to be intrepreted as a date instead of a number? > > Thanks, > ~Dave R. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595