0

我正在学习R,并且我有一个数据集,其中包含大约 6,000 条三个天气变量的每日记录。我有兴趣了解每年每个季节的每个变量的平均值和标准差。我发现了很多不同的方法来将数据分解成子集。但是,如果不为每一个输入mean(s1997$tmax)& ,我无法弄清楚如何计算这些。sd(s1997$tmax)我想制作如下所示的东西:

Season    mean.TMAX   sd.TMAX   mean.TMIN   sd.TMIN
s1997
f1997
w1997
s1998

我什至不确定R可以做到这一点。

4

2 回答 2

1

在没有数据的情况下未经测试:

aggregate( s1997[, c("TMAX", "TMIN") ], 
           s1997["Season"], 
           function(x) c(mean=mean(x), sd=sd(x) ) )

考试:

aggregate(data.frame(state.x77), list(Region = state.region), 
                                 function(x) c( mean= mean(x), sd=sd(x) ) )
         Region Population.mean Population.sd Income.mean Income.sd
1     Northeast        5495.111      6079.565   4570.2222  559.0771
2         South        4208.125      2779.508   4011.9375  605.4505
3 North Central        4803.000      3702.828   4611.0833  283.0825
4          West        2915.308      5578.607   4702.6154  663.9004
  Illiteracy.mean Illiteracy.sd Life.Exp.mean Life.Exp.sd Murder.mean
1       1.0000000     0.2783882    71.2644444   0.7438769    4.722222
2       1.7375000     0.5524189    69.7062500   1.0221994   10.581250
3       0.7000000     0.1414214    71.7666667   1.0367285    5.275000
4       1.0230769     0.6084870    71.2346154   1.3519715    7.215385
  Murder.sd HS.Grad.mean HS.Grad.sd Frost.mean  Frost.sd Area.mean
1  2.670570    53.966667   3.928104  132.77778  30.89408  18141.00
2  2.627602    44.343750   5.741773   64.62500  31.30682  54605.12
3  3.569345    54.516667   3.622865  138.83333  23.89307  62652.00
4  2.675770    62.000000   3.500000  102.15385  68.87652 134463.00
    Area.sd
1  18075.69
2  57965.27
3  14967.02
于 2013-10-30T05:51:31.177 回答
0

如果您不将数据分解为子集,那么您可以使用该data.table库 -

library(data.table)
RawData <- data.table(
Season = c("s1997","s1997","s1997","s1997","f1997","f1997","f1997","f1997"),
TMAX = rnorm(40,8),
TMIN = rnorm(10,8)
)
MeanData <- RawData[,
   list(
      TMAXmean = mean(TMAX),
      TMINmean = mean(TMIN)
   ),
   by = "Season"
]

RawData看起来像这样-

> RawData
   Season     TMAX      TMIN
1:  s1997 40.87113 10.474463
2:  s1997 40.16536  9.437184
3:  s1997 41.39432  9.562281
4:  s1997 40.08575 10.357866
5:  f1997 39.14282  8.880483
6:  f1997 39.47253 10.674257
7:  f1997 38.30043 10.493501
8:  f1997 38.16162 10.758552

像这样的输出 -

> MeanData
   Season TMAXmean  TMINmean
1:  s1997 40.62914  9.957948
2:  f1997 38.76935 10.201699
于 2013-10-30T07:06:53.723 回答