[R] date.mdy in date package
Spencer Graves
spencer.graves at pdf.com
Sun Jun 11 02:18:00 CEST 2006
Evidently, your 'trimmed_dates' was NOT a "a Julian date value, as
returned by 'mdy.date()', number of days since 1/1/1960."
My standard references for this kind of thing are the "zoo" vignette
and the R News article, Gabor Grothendieck and Thomas Petzoldt. R help
desk: Date and time classes in R. R News, 4(1):29-32, June 2004
(www.r-project.org -> Documentation: Newsletter). For more on
vignettes and "zoo" in particular, see
"http://finzi.psych.upenn.edu/R/Rhelp02a/archive/67006.html".
With my limited knowledge of date-time formates, I might approach
your problem as follows:
> Lines <-
+ "1 1/2/2003 2 1/3/2003 3 1/6/2003 4 1/7/2003 5
1/8/2003 6 1/9/2003 7 1/10/2003 8 1/13/2003 9 1/14/2003"
> (DF <- read.table(textConnection(Lines)))
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
V12 V13
1 1 1/2/2003 2 1/3/2003 3 1/6/2003 4 1/7/2003 5 1/8/2003 6
1/9/2003 7
V14 V15 V16 V17 V18
1 1/10/2003 8 1/13/2003 9 1/14/2003
> (Dates <- sapply(DF[2*(1:9)], as.character))
V2 V4 V6 V8 V10 V12
"1/2/2003" "1/3/2003" "1/6/2003" "1/7/2003" "1/8/2003" "1/9/2003"
V14 V16 V18
"1/10/2003" "1/13/2003" "1/14/2003"
> (Dates. <- strptime(Dates, "%m/%d/%Y"))
[1] "2003-01-02" "2003-01-03" "2003-01-06" "2003-01-07" "2003-01-08"
[6] "2003-01-09" "2003-01-10" "2003-01-13" "2003-01-14"
> class(Dates.)
[1] "POSIXt" "POSIXlt"
> names(Dates.)
[1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst"
>
Thus, the 'POSIXlt' format is similar to the output of 'date.mdy',
except that it has other attributes, which you could ignore. Getting
"month / days with leading zeros" is a formatting issue. If you still
can't figure that out after studying the Grothendieck and Petzoldt
article and the zoo vignette, please submit another post.
hope this helps.
Spencer Graves
Brian Scholl wrote:
> I'm having a problem with output from date.mdy in the date package.
>
> Goal: to take a long vector of dates of the form "01/22/99" and extract values month="01", day="22", year="1999".
>
> I am providing the vector of class dates in the attached file to date.mdy:
>
> > mdy_dates<-date.mdy(trimmed_dates)
>
> The first few obs of the attached vector of dates are:
> 1 1/2/2003 2 1/3/2003 3 1/6/2003 4 1/7/2003 5 1/8/2003 6 1/9/2003 7 1/10/2003 8 1/13/2003 9 1/14/2003
>
> I am expecting to get a list with vectors for the day, month, and year. The function appears to spit out the format I expect (though I would like months/days with leading zeros) but incorrect values, But what I get in the first few lines is something like:
>
> > mdy_dates
> $month
> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2
> [26] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
> [51] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4
> $day
> [1] 1 2 5 6 7 8 9 12 13 14 15 16 19 20 21 22 23 26 27 28 29 30 2 3 4
> [26] 5 6 9 10 11 12 13 16 17 18 19 20 23 24 25 26 27 2 3 4 5 6 9 10 11
> [51] 12 13 16 17 18 19 20 23 24 25 26 27 30 31 1 2 3 6 7 8 9 10 13 14 15
> $year
> [1] 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993
> [16] 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993
> [31] 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993
>
> So: The first day of my list of dates is Jan 2, 2003, but in the list output the first date is Jan 1, 1993. Have I incorrectly formatted my input, or is there some other problem?
>
> Thanks in advance.
>
> __________________________________________________
>
>
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> 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
More information about the R-help
mailing list