4

我是“R”的新用户,我找不到一个好的解决方案来解决它。我得到了以下格式的时间序列:

>dates  temperature depth   salinity
>12/03/2012 11:26   9.7533  0.48073 37.607
>12/03/2012 11:56   9.6673  0.33281 37.662
>12/03/2012 12:26   9.6673  0.33281 37.672

我对可变测量的频率不规则,根据时间段每 15 分钟或每 30 分钟进行一次。我想计算每个变量的年、月和日平均值,无论一天/月/年的数据数量是多少。我阅读了很多关于软件包 zoo、timeseries、xts 等的内容,但我无法清楚地了解我需要什么(可能是因为我对 R 的熟练程度不够……)。

我希望我的帖子很清楚,如果不是,请不要犹豫告诉我。

4

4 回答 4

6

将您的数据转换为 xts 对象,然后使用apply.dailyet al 计算您想要的任何值。

library(xts)
d <- structure(list(dates = c("12/03/2012 11:26", "12/03/2012 11:56", 
"12/03/2012 12:26"), temperature = c(9.7533, 9.6673, 9.6673), 
    depth = c(0.48073, 0.33281, 0.33281), salinity = c(37.607, 
    37.662, 37.672)), .Names = c("dates", "temperature", "depth", 
"salinity"), row.names = c(NA, -3L), class = "data.frame")
x <- xts(d[,-1], as.POSIXct(d[,1], format="%m/%d/%Y %H:%M"))
apply.daily(x, colMeans)
#                     temperature     depth salinity
# 2012-12-03 12:26:00    9.695967 0.3821167   37.647
于 2013-07-15T15:47:48.163 回答
3

我会将日、月和年添加到数据框中,然后使用aggregate().

首先将您的date列转换为 POSIXct 对象:

d$timestamp <- as.POSIXct(d$dates,format = "%m/%d/%Y %H:%M",tz ="GMT")

然后将日期(例如 12/03/2012)放入名为 的列Date中,试试这个:

d$Date <- format(d$timestamp,"%y-%m-%d",tz = "GMT")

接下来,按日期汇总:

aggregate(cbind("temperature.mean" = temperature,
                "salinity.mean" = salinity) ~ Date,
          data = d,
          FUN = mean)

同样,您可以将月份放入一列(我们称之为M月份),然后...

d$M <- format(d$timestamp,"%B",tz = "GMT")

aggregate(cbind("temperature.mean" = temperature,
                "salinity.mean" = salinity) ~ M,
          data = d,
          FUN = mean)

或者如果你想要年月

d$YM <- format(d$timestamp,"%y-%B",tz = "GMT")

aggregate(cbind("temperature.mean" = temperature,
                "salinity.mean" = salinity) ~ YM,
          data = d,
          FUN = mean)

如果您的数据中有任何 NA 值,您可能需要考虑这些:

aggregate(cbind("temperature.mean" = temperature,
                "salinity.mean" = salinity) ~ YM,
          data = d,
          function(x) mean(x,na.rm = TRUE))

最后,如果你想按周平均,你也可以这样做。先生成周数,然后再使用aggregate()

d$W <- format(d$timestamp,"%W",tz = "GMT")

aggregate(cbind("temperature.mean" = temperature,
                "salinity.mean" = salinity) ~ W,
          data = d,
          function(x) mean(x,na.rm = TRUE))

此版本的周数将第 1 周定义为一年中第一个星期一的那一周。星期是从星期一到星期日。

于 2013-07-15T15:56:15.770 回答
1

然而,另一种使用 plyr 的方法:

df <- structure(list(dates = c("12/03/2012 11:26", "12/03/2012 11:56", 
   "12/03/2012 12:26"), temperature = c(9.7533, 9.6673, 9.6673), 
   depth = c(0.48073, 0.33281, 0.33281), salinity = c(37.607, 
   37.662, 37.672)), .Names = c("dates", "temperature", "depth",                                                                                                
  "salinity"), row.names = c(NA, -3L), class = "data.frame")

library(plyr)

# Change date to POSIXct
df$dates <- with(d,as.POSIXct(dates,format="%m/%d/%Y %H:%M"))

# Make new variables, year and month
df <- transform(d,month=as.numeric(format(dates,"%m")),year=as.numeric(format(dates,"%Y")))

## According to year
ddply(df,.(year),summarize,meantemp=mean(temperature),meandepth=mean(depth),meansalinity=mean(salinity))
  year meantemp meandepth meansalinity
1 2012 9.695967 0.3821167       37.647

## According to month
ddply(df,.(month),summarize,meantemp=mean(temperature),meandepth=mean(depth),meansalinity=mean(salinity))
  month meantemp meandepth meansalinity
1    12 9.695967 0.3821167       37.647
于 2013-07-15T16:22:10.187 回答
1

该软件包hydroTSM具有创建年度和其他摘要的多种功能:

daily2annual(x, ...)
subdaily2annual(x, ...)
monthly2annual(x, ...)
annualfunction(x, FUN, na.rm = TRUE, ...)
于 2013-09-20T16:30:20.090 回答