Skip to content
Prev 359999 / 398503 Next

Query about use of format in strptime

Dear Jim and dear Enrico,
thank you for your replies.
Unfortunately your hints didn't solve my problem, and I am getting mad.
Can I show you my whole process? I will be as quick as possible.
I start from a data frame called Snow of the form

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
...

whth semi-hourly data.
I need to deal with date so I used strptime:

Snow$data_factor <- as.factor(paste(Snow$year, Snow$month, Snow$day, Snow$hh, Snow$mm, sep="-"))
Snow$data_strptime <- strptime(Snow$data_factor, format = "%Y-%m-%d-%H-%M")

It gives me

year month day hh mm hs  data_factor  data_strptime
1     2007    11  19  0  0  0  2007-11-19-0-0 2007-11-19 00:00:00
2     2007    11  19  0 30  0  2007-11-19-0-30  2007-11-19 00:30:00
3     2007    11  19  1  0  0  2007-11-19-1-0  2007-11-19 01:00:00
4     2007    11  19  1 30  0  2007-11-19-1-30  2007-11-19 01:30:00
5     2007    11  19  2  0  0  2007-11-19-2-0  2007-11-19 02:00:00
6     2007    11  19  2 30  0  2007-11-19-2-30  2007-11-19 02:30:00
7     2007    11  19  3  0  0  2007-11-19-3-0  2007-11-19 03:00:00
8     2007    11  19  3 30  0  2007-11-19-3-30  2007-11-19 03:30:00
9     2007    11  19  4  0  0  2007-11-19-4-0  2007-11-19 04:00:00
10   2007    11  19  4 30  0  2007-11-19-4-30  2007-11-19 04:30:00
...

The type of the column data_strptime is
$data_strptime
[1] "POSIXlt" "POSIXt"

Because of some days (or part of them) might be missing, given a time interval I want to create a new data frame with all time-steps and then merge the new data frame with the old one.
In order to create a new data frame with all time-steps, I thought to use

df_new <- data.frame(data_strptime=seq(init_day, fin_day, by="30 mins"))

and then

Snow_all <- merge(df_new, Snow, by=("data_strptime"), all.x=TRUE)

My problem is in dealing with init_day and fin_day, respectively for example "200711190000" and "200711210000".
I am not able to create a sequence of class "POSIXlt" "POSIXt", in order to merge the two data frames.

Could you please help me in this?
Thank you again for your attention
Stefano