简体   繁体   English

R:计算每天、每月和每年的连续小时数

[英]R: Counting number of consecutive hours per day, month, and year

I have researched and tried many things.我研究并尝试了很多东西。 I am already using dplyr, tidyr, data.table, lubridate, and sqldf packages in my script.我已经在我的脚本中使用了 dplyr、tidyr、data.table、lubridate 和 sqldf 包。

Here is part of my dataframe .这是我的数据框的一部分。 I am trying to count how many consecutive hours occur per day, month, and year.我试图计算每天、每月和每年连续发生的小时数。 Some days may have two consecutive sets, such as 2 hours and 4 hours.有些日子可能有两个连续的集合,例如 2 小时和 4 小时。 I would like all consecutive hours, not just the maximum per day.我想要所有连续的时间,而不仅仅是每天的最大值。

Ideally, the final dataframe will have year, month, day, and number of consecutive hours.理想情况下,最终数据框将具有年、月、日和连续小时数。 Thanks in advance!提前致谢!

Here is the code I currently have which gives me doubles and only the max:这是我目前拥有的代码,它给了我双打并且只有最大值:

consec <- df1 %>%
  add_count((hour)!= 1, name = 'Count') %>%
  group_by(year,month,day) 
    df <- read.table(header = TRUE, text = 'Date   Time    flow   year  Month    Day     Hour
        1991-01-01 8:00   4370  1991  01  01  08
                     1991-01-01 9:00   4370  1991  01  01  09
                     1991-01-01 10:00  4370  1991  01  01  10
                     1991-01-01 11:00  4370  1991  01  01  11
                     1991-01-01 12:00  4370  1991  01  01  12
                     1991-01-01 13:00  4370  1991  01  01  13
                     1991-01-01 14:00  4370  1991  01  01  14
                     1991-01-01 15:00  4370  1991  01  01  15
                     1991-01-01 16:00  4370  1991  01  01  16
                     1991-01-01 17:00  4370  1991  01  01  17
                     1991-01-01 20:00  4370  1991  01  01  20
                     1991-01-02 8:00   4370  1991  01  02  08
                     1991-01-04 9:00   4370  1991  01  04  09
                     1991-01-04 10:00  4370  1991  01  04  10
                     1991-02-02 11:00  4370  1991  02  02  11
                     1991-02-02 12:00  4370  1991  02  02  12
                     1992-01-02 13:00  4370  1992  01  02  13
                     1992-01-02 14:00  4370  1992  01  02  14
                     ')
    df$year=as.numeric(df$year)
    df$Month=as.numeric(df$Month)
    df$Day=as.numeric(df$Day)
    df$Hour=as.numeric(df$Hour)
    df=df[order(df$year,df$Month,df$Day,df$Hour),]
    

df$obs=ifelse(df$year == c("NA",df$year[-nrow(df)]) 
                & df$Month==c("NA",df$Month[-nrow(df)])  
                & df$Day  ==c("NA",df$Day[-nrow(df)])  
                & df$Hour-1 == c("NA",df$Hour[-nrow(df)])
                ,1,0)

    
    df1=aggregate(df$obs, FUN=sum, by=list(Year=df$year, Month=df$Month, Day=df$Day))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM