Pergunta

I am quite new to R, and I am trying to find a way to average continuous data into a specific period of time.

My data is a month recording of several parameters with 1s time steps The table via read.csv has a date and time in one column and several other columns with values.

TimeStamp UTC   Pitch   Roll    Heave(m)
05-02-13 6:45   0   0   0
05-02-13 6:46   0.75    -0.34   0.01
05-02-13 6:47   0.81    -0.32   0
05-02-13 6:48   0.79    -0.37   0
05-02-13 6:49   0.73    -0.08   -0.02

So I want to average the data in specific intervals: 20 min for example in a way that the average for hour 7:00, takes all the points from hour 6:41 to 7:00 and returns the average in this interval and so on for the entire dataset. The time interval will look like this :

TimeStamp   
05-02-13 19:00  462
05-02-13 19:20  332
05-02-13 19:40  15
05-02-13 20:00  10
05-02-13 20:20  42
Foi útil?

Solução

Here is a reproducible dataset similar to your own.

meteorological <- data.frame(
  TimeStamp = rep.int("05-02-13", 1440),
  UTC       = paste(
    rep(formatC(0:23, width = 2, flag = "0"), each = 60),
    rep(formatC(0:59, width = 2, flag = "0"), times = 24),
    sep = ":"
  ),
  Pitch     = runif(1440),
  Roll      = rnorm(1440),
  Heave     = rnorm(1440)
)

The first thing that you need to do is to combine the first two columns to create a single (POSIXct) date-time column.

library(lubridate)
meteorological$DateTime <- with(
  meteorological,
  dmy_hm(paste(TimeStamp, UTC))
)

Then set up a sequence of break points for your different time groupings.

breaks <- seq(ymd("2013-02-05"), ymd("2013-02-06"), "20 mins")

Finally, you can calculate the summary statistics for each group. There are many ways to do this. ddply from the plyr package is a good choice.

library(plyr)
ddply(
  meteorological,
  .(cut(DateTime, breaks)),
  summarise,
  MeanPitch = mean(Pitch),
  MeanRoll  = mean(Roll),
  MeanHeave = mean(Heave)
)

Outras dicas

Please see if something simple like this works for you:

myseq <- data.frame(time=seq(ISOdate(2014,1,1,12,0,0), ISOdate(2014,1,1,13,0,0), "5 min"))
myseq$cltime <- cut(myseq$time, "20 min", labels = F)

> myseq
                  time cltime
1  2014-01-01 12:00:00      1
2  2014-01-01 12:05:00      1
3  2014-01-01 12:10:00      1
4  2014-01-01 12:15:00      1
5  2014-01-01 12:20:00      2
6  2014-01-01 12:25:00      2
7  2014-01-01 12:30:00      2
8  2014-01-01 12:35:00      2
9  2014-01-01 12:40:00      3
10 2014-01-01 12:45:00      3
11 2014-01-01 12:50:00      3
12 2014-01-01 12:55:00      3
13 2014-01-01 13:00:00      4
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top