Plot time data in R to various resolutions (to the minute, to the hour, to the second, etc.)

后端 未结 2 794
走了就别回头了
走了就别回头了 2021-01-31 06:46

I have some data in CSV like:

\"Timestamp\", \"Count\"
\"2009-07-20 16:30:45\", 10
\"2009-07-20 16:30:45\", 15
\"2009-07-20 16:30:46\", 8
\"2009-07-20 16:30:46\"         


        
2条回答
  •  不要未来只要你来
    2021-01-31 07:14

    Averaging the data is easy with the plyr package.

    library(plyr)
    Second <- ddply(dataset, "Timestamp", function(x){
        c(Average = mean(x$Count), N = nrow(x))
    })
    

    To do the same thing by minute or hour, then you need to add fields with that info.

    library(chron)
    dataset$Minute <- minutes(dataset$Timestamp)
    dataset$Hour <- hours(dataset$Timestamp)
    dataset$Day <- dates(dataset$Timestamp)
    #aggregate by hour
    Hour <- ddply(dataset, c("Day", "Hour"), function(x){
        c(Average = mean(x$Count), N = nrow(x))
    })
    #aggregate by minute
    Minute <- ddply(dataset, c("Day", "Hour", "Minute"), function(x){
        c(Average = mean(x$Count), N = nrow(x))
    })
    

提交回复
热议问题