Why are some datetime values being deleted when I round the time in R? -
i importing series of csv files r. these contain date/time column, id, , 2 columns of temperature values.
this give example of data looks like:
id<-c(1,2,3,4) date.time<-as.character(c("12/03/17 00:21:28", "12/03/17 02:21:28", "12/03/17 04:21:28", "12/03/17 06:21:28")) temp1<-c(-3.568,-3.568,-3.598,-3.598) temp2<-c(-11.577,-11.577,-11.541,-11.433) df<-data.frame(id,date.time,temp1,temp2)
because date/time not in format want, i've been using strptime , formatting them posixlt.
like:
df$date.time<-strptime(df$date.time, "%d/%m/%y %h:%m:%s") df$date.time<- as.posixlt(df$date.time, "%y/%m/%d %h:%m:%s", tz="gmt0")
this works fine , gives data looks like:
id date.time temp1 temp2 1 2017-03-12 0:21:28 -3.568 -11.577 2 2017-03-12 2:21:28 -3.568 -11.577 3 2017-03-12 4:21:28 -3.598 -11.541 4 2017-03-12 6:21:28 -3.598 -11.433
however, want round time part of date.time column nearest hour. i've been using:
df$date.time<-round(df$date.time, units="hours")
this works fine 99% of time. however, in of files, r deleting date.time values, seemingly @ random, , giving na. 1 or 2 values in each file being deleted , can't see reason these particular values deleted. example:
id date.time temp1 temp2 1 2017-03-12 0:00:00 -3.568 -11.577 2 na -3.568 -11.577 3 2017-03-12 4:00:00 -3.598 -11.541 4 2017-03-12 6:00:00 -3.598 -11.433
from i've read, date/time values can finicky seems oddly random.
does know might causing issue , if there better way round time part of posixlt values?
update: seems times deleted @ 2am on march 12th. so, many of times should rounded 2017-03-12 02:00:00 being replaced nas. not happen of csv files, half. why r having problems reading particular date?
thanks!
not adding timezone strptime
corrupting string.
lets @ head
of df$date.time
. missing/different time zones.
head(df$date.time) [1] "2017-03-12 00:21:28 pst" [2] "2017-03-12 02:21:28" [3] "2017-03-12 04:21:28 pdt" [4] "2017-03-12 06:21:28 pdt"
by adding tz="gmt0"
strptime
function should desired result.
df$date.time<-strptime(df$date.time, "%d/%m/%y %h:%m:%s", tz="gmt0") df$date.time<- as.posixlt(df$date.time, "%y/%m/%d %h:%m:%s", tz="gmt0") df$date.time<-round(df$date.time, units="hours") > head(df$date.time) [1] "2017-03-12 00:00:00 gmt" [2] "2017-03-12 02:00:00 gmt" [3] "2017-03-12 04:00:00 gmt" [4] "2017-03-12 06:00:00 gmt"
Comments
Post a Comment