r - Calculating the median of a time series, by 8 every 8 hours -
i new r , have calculate mean of time series, containing 5 years, hourly taken data of ozon etc..
my df looks like:
structure(list(date = structure(c(1l, 1l, 1l, 1l), .label = "01.01.2010", class = "factor"), day.of = c(1l, 1l, 1l, 1l), time = structure(1:4, .label = c("00:00", "01:00", "02:00", "03:00"), class = "factor"), svf_ray = c(1l, 1l, 1l, 1l), gmax = c(0, 0, 0, 0), ta = c(-1.3, -1.2, -1.2, -1.2), tmrt = c(-19.3, -12.1, -12, -12.1), pet = c(-10.4, -8.7, -8.7, -8.7), pt = c(-11.3, -9.3, -9.3, -9.3), ozon = c(61.35, 62.65, 63.4, 63.85), rdatum = structure(c(14610, 14610, 14610, 14610), class = "date"), year = c(2010, 2010, 2010, 2010), month = c(1, 1, 1, 1), day = c(1, 1, 1, 1), hour = c(0, 1, 2, 3)), .names = c("date", "day.of", "time", "svf_ray", "gmax", "ta", "tmrt", "pet", "pt", "ozon", "rdatum", "year", "month", "day", "hour"), row.names = c(na, 4l), class = "data.frame")
i calculate mean of ozon every 8 hours, series of 4 calculated means every day. have arranged datum like:
datum_ozon$rdatum <- as.date(data$date, format="%d.%m.%y") datum_ozon$hour<-as.numeric(unlist(strsplit(as.character(df$time), ":"))[seq(1, 2 * length(df$time), 2)])
format numeric
but don't know further in achieving goal. in advance!
if case data regular , complete (ie, every hour has record), following base r code should trick:
# number of 8 hour intervals intervalcnt <- nrow(df) / 8l # add grouping vector data df$group <- rep(1:intervalcnt, each=8) # median each interval, keep year var around later intervalmedian <- aggregate(var~group + day + month + year, data=df, fun=median)
note solution relies on assumption data has regular structure, i.e., every hour has record. if measure of interest missing, i.e. na, adding na.rm aggregate function return statistics of interest:
# median each interval intervalmedian <- aggregate(var~group + day + month + year, data=df, fun=median, na.rm=t)
if have variable hour of day, here simple way check data regularity:
table(df$hourofday)
the result of function frequency count of each hour. counts should equal. thing check first observation starts in hour following final observation, i.e. if hour of observation 1 == "00:00", hour of final observation should 23:00.
to provide plot of mean of 8 hour periods year, can again use aggregate:
intervalmeans.year <- aggregate(var~group, data=intervalmedian, fun=mean, na.rm=t)
the inclusion of group, day, month, , year variables in intervalmedian data.frame allow lot of different aggregations. example, minor adjustment, possible average value of variable on 5 year period each time period-day-month:
intervalmedian$periodday <- rep(1:3, length.out=intervalmedian) intervalmeans.daymonthperiod <- aggregate(var~periodday+day+month, data=intervalmedian, fun=mean, na.rm=t)
Comments
Post a Comment