I would like to use POSIX classes to store dates and extract components of dates. Following the example in Spector ("Data Manipulation in R"), I create a date> mydate = as. POSIXlt('2005-4-19 7:01:00')I then successfully extract the day with the command> mydate$day[1] 19 But when I try to extract the month > mydate$mon [1] 3 it returns the wrong month. And mydate$year is off by about 2,000 years. Am I doing something wrong? Dr. Seth W. Bigelow Biologist, USDA-FS Pacific Southwest Research Station 1731 Research Park Drive, Davis California sbigelow@fs.fed.us / ph. 530 759 1718 [[alternative HTML version deleted]]
Month counts from 0 in POSIXlt objects, so that April is month 3 in your example, January being month 0. Year counts from 1900 in POSIXlt objects, so that 2005 should return as 105 in your example. All of the other fields in POSIXlt should return values that you might expect them to a priori. Keep an eye on daylight savings time adjustments if they apply in your time zone. On Tue, Mar 1, 2011 at 12:01 PM, Seth W Bigelow <sbigelow@fs.fed.us> wrote:> I would like to use POSIX classes to store dates and extract components of > dates. Following the example in Spector ("Data Manipulation in R"), I > create a date > > > mydate = as. POSIXlt('2005-4-19 7:01:00') > > I then successfully extract the day with the command > > > mydate$day > [1] 19 > > But when I try to extract the month > > > mydate$mon > [1] 3 > > it returns the wrong month. And mydate$year is off by about 2,000 years. > Am I doing something wrong? > > Dr. Seth W. Bigelow > Biologist, USDA-FS Pacific Southwest Research Station > 1731 Research Park Drive, Davis California > sbigelow@fs.fed.us / ph. 530 759 1718 > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- ___________________________ Luke Miller Postdoctoral Researcher Marine Science Center Northeastern University Nahant, MA [[alternative HTML version deleted]]
Prof Brian Ripley
2011-Mar-01 18:14 UTC
[R] Does POSIXlt extract date components properly?
On Tue, 1 Mar 2011, Seth W Bigelow wrote:> I would like to use POSIX classes to store dates and extract components of > dates. Following the example in Spector ("Data Manipulation in R"), I > create a date > >> mydate = as. POSIXlt('2005-4-19 7:01:00') > > I then successfully extract the day with the command > >> mydate$day > [1] 19 > > But when I try to extract the month > > > mydate$mon > [1] 3 > > it returns the wrong month. And mydate$year is off by about 2,000 years. > Am I doing something wrong?Not reading the documentation (nor the posting guide). ?DateTimeClasses says ?mon? 0-11: months after the first of the year. ?year? years since 1900. That is the POSIX standard ... you could also have looked there.> Dr. Seth W. Bigelow > Biologist, USDA-FS Pacific Southwest Research Station > 1731 Research Park Drive, Davis California > sbigelow at fs.fed.us / ph. 530 759 1718 > [[alternative HTML version deleted]]Please note what the posting guide said about that!> ______________________________________________ > 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. >-- 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