NiPapen
NiPapen

Reputation: 95

Milliseconds in POSIXtc

My data I need to analyse looks like this:

df<-data.frame(U$V2,U$V3) head(df)

                         U.V2  U.V3
1  2010-03-01 13:02:00.001007 0.030
2 2010-03-01 13:02:00.0020141 0.031
3 2010-03-01 13:02:00.0030059 0.035
4  2010-03-01 13:02:00.004013 0.041
5 2010-03-01 13:02:00.0050048 0.049
6 2010-03-01 13:02:00.0060119 0.060

There are 250 measurements in one minute time interval. What I would like to have are the measurements on every 5 minutes interval. I tried to play with the POSIXct but it is not working. It is returning minute data.

When I try to change my df DateTime in the POSIXct format.

DateTimeU<-as.POSIXct(U$V2, format = "%Y-%m-%d %H:%M:%S")

The output I get is:

"2010-03-01 13:02:00 CET" "2010-03-01 13:02:00 CET" "2010-03-01 13:02:00 CET" "2010-03-01 13:02:00 CET" "2010-03-01 13:02:00 CET" "2010-03-01 13:02:00 CET"

Can somebody suggest a package or an approach that should work that the output would be 250 values on every 5 minutes starting from the first 5 min value 2010-03-01 13:05:00 and so on.

Upvotes: 1

Views: 180

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368241

You are missing the options(digits.secs=6) setting.

Demo

R> data <- read.csv(text="dt,val
+ 2010-03-01 13:02:00.0010070, 0.030
+ 2010-03-01 13:02:00.0020141, 0.031
+ 2010-03-01 13:02:00.0030059, 0.035
+ 2010-03-01 13:02:00.0040130, 0.041
+ 2010-03-01 13:02:00.0050048, 0.049
+ 2010-03-01 13:02:00.0060119, 0.060")
R>
R> data[,1] <- anytime::anytime(data[, 1])  ## or as.POSIXct or ...
R>
R> options(digits.secs=6)
R> data
                           dt   val
1 2010-03-01 13:02:00.0010070 0.030
2 2010-03-01 13:02:00.0020141 0.031
3 2010-03-01 13:02:00.0030059 0.035
4 2010-03-01 13:02:00.0040130 0.041
5 2010-03-01 13:02:00.0050048 0.049
6 2010-03-01 13:02:00.0060119 0.060
R> 
R> class(data[,1])
[1] "POSIXct" "POSIXt" 
R> 

The zoo package has a few fine vignettes showing to slice, dice and aggregate data based on timestamps.

Upvotes: 2

Related Questions