hello, I have a vector of factors> str(rcptdt)Factor w/ 51 levels "1/10/03","1/13/03",..:> length(rcptdt)[1] 87 which i want to convert to class POSIXlt to extract the day, so: a1<-format(rcptdt,"%m/%d/%y")> length(a1)[1] 87 and: a2<-strptime(a1, "%m/%d/%y") str(a2) `POSIXlt', format: chr [1:87] "2002-04-18" "2002-07-19" "2002-08-02" "2002-08-14" ...> a2[1]-a2[2]Time difference of -92 days but the length differs> length(a2)[1] 9 and i cant update rcptdt...> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 1 minor 8.1 year 2003 month 11 day 21 language R thanks, amer research analyst disability det. bureau madtown-wi-usa
"Siddique, Amer" <Amer.Siddique at ssa.gov> writes:> hello, > I have a vector of factors > > str(rcptdt) > Factor w/ 51 levels "1/10/03","1/13/03",..: > > length(rcptdt) > [1] 87 > > which i want to convert to class POSIXlt to extract the day, so: > a1<-format(rcptdt,"%m/%d/%y")Eh? Why? A simple as.character() should do it. The format string has no effect.> > length(a1) > [1] 87 > > and: > a2<-strptime(a1, "%m/%d/%y") > str(a2) > `POSIXlt', format: chr [1:87] "2002-04-18" "2002-07-19" "2002-08-02" > "2002-08-14" ... > > a2[1]-a2[2] > Time difference of -92 days > > but the length differs > > length(a2) > [1] 9 > > and i cant update rcptdt...as.POSIXct(...) is needed, I believe. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
These are the 9 elements of POSIXlt. For example, try this to see the 9 components: unclass(a2) The day of the month, which is what you are looking for, is a2$mday . If you want an 87 element vector of dates you probably want to store them as POSIXct: length(as.POSIXct(a2)) or as chron dates: require(chron) length(chron(as.character(rcptdt))) Note that you could also get the day of the month this way: require(chron) month.day.year(chron(as.character(rcptdt)))$day Also note that your format call can be shortened to format(rcptdt) or as.character(rcptdt) since, at that point, you are not yet dealing with dates. --- Date: Fri, 6 Feb 2004 14:10:46 -0600 From: Siddique, Amer <Amer.Siddique at ssa.gov> To: R (r-help at stat.math.ethz.ch) <r-help at stat.math.ethz.ch> Subject: [R] vector of factors to POSIXlt hello, I have a vector of factors> str(rcptdt)Factor w/ 51 levels "1/10/03","1/13/03",..:> length(rcptdt)[1] 87 which i want to convert to class POSIXlt to extract the day, so: a1<-format(rcptdt,"%m/%d/%y")> length(a1)[1] 87 and: a2<-strptime(a1, "%m/%d/%y") str(a2) `POSIXlt', format: chr [1:87] "2002-04-18" "2002-07-19" "2002-08-02" "2002-08-14" ...> a2[1]-a2[2]Time difference of -92 days but the length differs> length(a2)[1] 9 and i cant update rcptdt...> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 1 minor 8.1 year 2003 month 11 day 21 language R thanks, amer research analyst disability det. bureau madtown-wi-usa