I am trying to work with the Date class which is written in S3 and I would like to access to the elements of the class (for example the year). I've tryed to do it for example like this: as.Date(Sys.time)->w w$year #Doesn't work w["year"] #is NA I would like to know the correct way to acces to this value. Thank you so much -- Elisenda Vila [[alternative HTML version deleted]]
On Jul 7, 2010, at 7:25 AM, Elisenda Vila wrote:> I am trying to work with the Date class which is written in S3 and I > would > like to access to the elements of the class (for example the year). > I've > tryed to do it for example like this: > > as.Date(Sys.time)->wThrows an error ... since Sys.time is a function> w$year #Doesn't workWhy should it? Dates are not stored as lists. ?Date> w["year"] #is NA > > I would like to know the correct way to acces to this value.as.Date(Sys.time)->w Error in as.Date.default(Sys.time) : do not know how to convert 'Sys.time' to class "Date" > as.Date(Sys.time())->w > w [1] "2010-07-07" > format(w, "%Y") [1] "2010" -- David Winsemius, MD West Hartford, CT
On Wed, Jul 07, 2010 at 01:25:43PM +0200, Elisenda Vila wrote:> I am trying to work with the Date class which is written in S3 and I would > like to access to the elements of the class (for example the year). I've > tryed to do it for example like this: > > as.Date(Sys.time)->ww <- Sys.Date() # does the same, but in one step> w$year #Doesn't work > w["year"] #is NA > > I would like to know the correct way to acces to this value.wp <- as.POSIXlt(w) # POSIXlt has the components unclass(wp) # shows you all components wp$year + 1900 # stored as year - 1900, see Unix manuals wp$mon + 1 # stores as mon -1, see Unix manuals Arguably, extractor functions would be of help here. -- Regards, Dirk