Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于UTC时间创建组_R_Mean_Utc_Posixct - Fatal编程技术网

R 基于UTC时间创建组

R 基于UTC时间创建组,r,mean,utc,posixct,R,Mean,Utc,Posixct,我有一个如下所示的数据集: str(m12)'data.frame': 48178 obs. of 10 variables: $ created_utc : POSIXct, format: "2016-04-19 02:59:02" "2016-05-01 01:51:58" "2016-04-20 15:11:24" "2016-04-26 23:09:13" ... $ WC : int 122 24 27 34 43 30 18 49 52 16 ...

我有一个如下所示的数据集:

str(m12)'data.frame':   48178 obs. of  10 variables:
$ created_utc  : POSIXct, format: "2016-04-19 02:59:02" "2016-05-01 01:51:58" "2016-04-20 15:11:24" "2016-04-26 23:09:13" ...
$ WC           : int  122 24 27 34 43 30 18 49 52 16 ...
$ Analytic     : num  74.05 6.55 1.32 26.21 11.64 ...
$ Clout        : num  20.6 1 35.5 38.4 40.8 ...
$ Authentic    : num  80.8 91.3 92.5 14.7 87.5 ...
....
我想计算每一天每个变量的平均分数

我试过这个:

mean <- aggregate(m12[, 2:10], list(m12$created_utc), mean)

mean我们需要将“created_utc”转换为
Date
类,以便去除时间部分。然后,将其用作分组变量,以获取单日每列的
平均值

aggregate(.~cbind( created_utc= as.Date(created_utc)), m12, FUN = mean, 
          na.rm = TRUE, na.action = NULL)

更快的方法是使用
dplyr
数据。表

library(dplyr) 
m12 %>%
    group_by(created_utc = as.Date(created_utc)) %>%
    summarise_each(funs(mean= mean(., na.rm = TRUE)))


它工作顺利。我使用了带有dplyr的版本。非常感谢。
setDT(m12)[, lapply(.SD, mean, na.rm = TRUE) , .(created_utc = as.Date(created_utc))]