Aggregation by time period in lubridate

后端 未结 4 512
青春惊慌失措
青春惊慌失措 2021-01-05 11:18

This question asks about aggregation by time period in R, what pandas calls resampling. The most useful answer uses the XTS package to group by a given time period, applying

4条回答
  •  温柔的废话
    2021-01-05 12:10

    Here is an option using data.table after importing the csv:

    library(data.table)
    
    # convert the data.frame to data.table
    bikecounts <- data.table(bikecounts)
    
    # Calculate
    bikecounts[, list(NB=sum(Northbound), SB=sum(Southbound)), by=as.Date(Date, format="%m/%d/%Y")]
    
            as.Date   NB   SB
      1: 2012-10-02 1165  773
      2: 2012-10-03 1761 1760
      3: 2012-10-04 1767 1708
      4: 2012-10-05 1590 1558
      5: 2012-10-06  926 1080
     ---                     
    299: 2013-07-27 1212 1289
    300: 2013-07-28  902 1078
    301: 2013-07-29 2040 2048
    302: 2013-07-30 2314 2226
    303: 2013-07-31 2008 2076
    

    Note, you can also use fread() ("fast read") from the data.table package to read in the CSV into a data.table in one step. The only draw back is you to manually convert the date/time from string.

    eg: 
    bikecounts <- fread("http://data.seattle.gov/api/views/65db-xm6k/rows.csv?accessType=DOWNLOAD", header=TRUE, stringsAsFactors=FALSE)
    setnames(bikecounts, c("Date", "Northbound", "Southbound"))
    bikecounts[, Date := as.POSIXct(D, format="%m/%d/%Y %I:%M:%S %p")] 
    

提交回复
热议问题