Summing rows by month in R

后端 未结 4 561
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 05:55

So I have a data frame that has a date column, an hour column and a series of other numerical columns. Each row in the data frame is 1 hour of 1 day for an entire year.

4条回答
  •  误落风尘
    2020-12-06 06:44

    This could be another way to do this using data.table

    library(data.table)
    # Edited as per Arun's comment
    out = setDT(data)[, lapply(.SD, sum), by=Date] 
    
    #>out
    #         Date Hour Melbourne Southern Flagstaff
    #1: 2009-05-01   21         0      496       715
    

    or by using dplyr

    library(dplyr)
    out = data %>% group_by(Date) %>% summarise_each(funs(sum))
    
    #>out
    #Source: local data frame [1 x 5]
    #        Date Hour Melbourne Southern Flagstaff
    #1 2009-05-01   21         0      496       715
    

提交回复
热议问题