我需要通过将总和将小时数据转换为每日数据 [英] I need to convert my hourly data to daily data by taking the sum

查看:148
本文介绍了我需要通过将总和将小时数据转换为每日数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据集:

structure(list(time = structure(c(1506406740, 1506406770, 1506406860, 
1506406890, 1506406920, 1506406950, 1506406980, 1506407010, 1506407040, 
1506407070), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
Column3 = c(131, 131, 131, 131, 131, 131, 131, 131, 131, 
131), m_Pm = c(2.402842, 2.556558, 2.805165, 2.97428, 3.101824, 
3.23984, 3.359587, 3.474448, 3.62753, 3.773597)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame")) 

在第一个聚合函数之前都可以正常工作。之后,求和函数后我无法获得正确的答案。我正在获取较大的值

It works alright till the first aggregate function . after that I am not able to get the correct answer after the sum function . I'm getting large values instead

library(dplyr)
library(ggplot2)

attach(data)
data %>% #filtering 131 and 132
  select(time,Column3,m_Pm) %>%
  filter(data,Column3=="131") 
filter(data,Column3=="132")
data_131<-filter(data,Column3=="131") 
data_132<-filter(data,Column3=="132") 

#datehour column (dailyaverage)
data_131$datehour<-format(data_131$time,"%Y-%m-%d %H")
aggregate(m_Pm~datehour,data_131, mean)

#datecolumn

data_131$date1<-format(data_131$time,"%Y-%m-%d")
dE_131<-aggregate(m_Pm~date1,data_131,sum)
dE_131}

数据太大,我无法在此处发布。
这是每30秒7个月的数据。

Since the data is too huge, I cannot post it here. It is a 7 months data of every 30 seconds.

推荐答案

library(dplyr)
df %>% mutate(dayH= format(time,"%Y-%m-%d %H"), day= format(time,"%Y-%m-%d")) %>% 
       group_by(dayH) %>% mutate(th=mean(m_Pm)) %>% distinct(dayH, .keep_all = TRUE) %>% 
       group_by(day) %>% summarise(tday=sum(th))

# A tibble: 1 x 2
  day         tday
  <chr>      <dbl>
1 2017-09-26  15.7


df %>% mutate(dayH= format(time,"%Y-%m-%d %H"), day= format(time,"%Y-%m-%d"), month=format(time,"%Y-%m")) %>% 
       group_by(dayH) %>% mutate(th=mean(m_Pm)) %>% distinct(dayH, .keep_all = TRUE) %>% 
       group_by(day) %>% mutate(tday=sum(th)) %>% distinct(day, .keep_all = TRUE) %>% 
       group_by(month) %>% summarise(tmonth=sum(tday))

这篇关于我需要通过将总和将小时数据转换为每日数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆