计算数据帧中值的平均值和总和

时间:2015-10-13 17:48:16

标签: r sum data.table mean

          DATETIME      MAX.TEMP MIN.TEMP NEW.PRCP NEW.SNOW
18791 1999/03/20 06:00      0.4     -2.0        0      0.0
18911 1999/03/20 18:00     11.7     -1.5        0      0.0
19031 1999/03/21 06:00      6.9      2.0       -1      0.1
19151 1999/03/21 18:00     12.3      2.4       -1      0.0
19271 1999/03/22 06:00      2.6     -1.1        1      0.0
19391 1999/03/22 18:00      5.4     -2.3        0      0.0
19511 1999/03/23 06:00      1.1     -3.9        1      0.1
19631 1999/03/23 18:00      7.1     -3.1        0      0.1
19751 1999/03/24 06:00      1.6     -2.6        0      0.1
19871 1999/03/24 18:00      7.5     -0.8        1      0.1

我想得到列的每日均值:MAX.TEMP,MIN.TEMP 另外,我想得到列的每日总和:NEW.PRCP,NEW.SNOW

我一直在尝试使用类似于早期问题here

的data.table

我开始尝试模仿提供的评论,并开始尝试获得MAX.TEMP的平均值:

setDT(new_Tbl)[, .(DATETIME = DATETIME[1L], MAX.TEMP = mean(MAX.TEMP)), 
           by = .(indx = substr(DATETIME,12,13) == '06'))]

返回意外的错误]和)找不到函数"。"

我假设我没有遵循的语法中有快捷方式。我很感激帮助和耐心。

1 个答案:

答案 0 :(得分:1)

library(data.table)
setDT(df)[, `:=`(MAX.TEMP = mean(MAX.TEMP), MIN.TEMP = mean(MIN.TEMP), 
                 NEW.PRCP = sum(NEW.PRCP), NEW.SNOW = sum(NEW.SNOW)), as.IDate(DATETIME)]
# 
#             DATETIME MAX.TEMP MIN.TEMP NEW.PRCP NEW.SNOW
#  1: 1999/03/20 06:00     6.05    -1.75        0      0.0
#  2: 1999/03/20 18:00     6.05    -1.75        0      0.0
#  3: 1999/03/21 06:00     9.60     2.20       -2      0.1
#  4: 1999/03/21 18:00     9.60     2.20       -2      0.1
#  5: 1999/03/22 06:00     4.00    -1.70        1      0.0
#  6: 1999/03/22 18:00     4.00    -1.70        1      0.0
#  7: 1999/03/23 06:00     4.10    -3.50        1      0.2
#  8: 1999/03/23 18:00     4.10    -3.50        1      0.2
#  9: 1999/03/24 06:00     4.55    -1.70        1      0.2
# 10: 1999/03/24 18:00     4.55    -1.70        1      0.2

每列都在`:=`(...)运算符中创建。此外,它全部按天分组,无需显式创建新列as.IDate(DATETIME)