Skip to content
Prev 360008 / 398503 Next

Query about use of format in strptime

First I added one row to your data, to illustrate a case with missing
times:

year month day hh mm hs
2007 11 19 0 0 0.00
2007 11 19 0 30 0.00
2007 11 19 1 0 0.00
2007 11 19 1 30 0.00
2007 11 19 2 0 0.00
2007 11 19 2 30 0.00
2007 11 19 3 0 0.00
2007 11 19 3 30 0.00
2007 11 19 4 0 0.00
2007 11 19 4 30 0.00
2007 11 19 6 30 0.00

(and I put it in a separate file named snowday.dat)

Then try this:

sd <- read.table('snowday.dat', sep=' ', head=TRUE)
sd$tm <- as.POSIXct( paste(sd$year, sd$month, sd$day, sd$hh, sd$mm,
sep='-'), format='%Y-%m-%d-%H-%M')
dft <- data.frame( tm=seq(min(sd$tm), max(sd$tm), by='30 min') )
sd <- merge(sd, dft, all=TRUE)



This appears to do what you are asking for (if I understand correctly).
tm year month day hh mm hs
1  2007-11-19 00:00:00 2007    11  19  0  0  0
2  2007-11-19 00:30:00 2007    11  19  0 30  0
3  2007-11-19 01:00:00 2007    11  19  1  0  0
4  2007-11-19 01:30:00 2007    11  19  1 30  0
5  2007-11-19 02:00:00 2007    11  19  2  0  0
6  2007-11-19 02:30:00 2007    11  19  2 30  0
7  2007-11-19 03:00:00 2007    11  19  3  0  0
8  2007-11-19 03:30:00 2007    11  19  3 30  0
9  2007-11-19 04:00:00 2007    11  19  4  0  0
10 2007-11-19 04:30:00 2007    11  19  4 30  0
11 2007-11-19 05:00:00   NA    NA  NA NA NA NA
12 2007-11-19 05:30:00   NA    NA  NA NA NA NA
13 2007-11-19 06:00:00   NA    NA  NA NA NA NA
14 2007-11-19 06:30:00 2007    11  19  6 30  0



Notes:
There is no need to use factor()
As David said, don't use POSIXlt. Use POSIXct instead.