How to select and display hourly average data from a data frame?

I have a CSV file that looks like where "time" is the UNIX timestamp:

time,count
1300162432,5
1299849832,0
1300006132,1
1300245532,4
1299932932,1
1300089232,1
1299776632,9
1299703432,14
... and so on

I read it in R and convert the time columns to POSIXct like this:

data <- read.csv(file="data.csv",head=TRUE,sep=",")
data[,1] <- as.POSIXct(data[,1], origin="1970-01-01")

Great, but right now I would like to plot a histogram with each bin corresponding to the average hourly count. I was stuck in the selection by the clock, and then counted. I looked through ?POSIXtand ?cut.POSIXt, but if the answer is there, I do not see it.

Any help would be appreciated.

+3
source share
3 answers

Here is one way:

R> lines <- "time,count
1300162432,5
1299849832,0
1300006132,1
1300245532,4
1299932932,1
1300089232,1
1299776632,9
1299703432,14"
R> con <- textConnection(lines); df <- read.csv(con); close(con)
R> df$time <- as.POSIXct(df$time, origin="1970-01-01")
R> df$hour <- as.POSIXlt(df$time)$hour
R> df
                 time count hour
1 2011-03-15 05:13:52     5    5
2 2011-03-11 13:23:52     0   13
3 2011-03-13 09:48:52     1    9
4 2011-03-16 04:18:52     4    4
5 2011-03-12 12:28:52     1   12
6 2011-03-14 08:53:52     1    8
7 2011-03-10 17:03:52     9   17
8 2011-03-09 20:43:52    14   20
R> tapply(df$count, df$hour, FUN=mean)
 4  5  8  9 12 13 17 20 
 4  5  1  1  1  0  9 14 
R> 

, , POSIX. TZ- .

+3

"" , POSIXlt . , , :

date.to.hour <- function (vec)
{
    as.POSIXct(
        sapply(
            vec,
            function (x)
            {
                lt = as.POSIXlt(x)
                x - 60*lt$min - lt$sec
            }),
        tz="GMT",
        origin="1970-01-01")
}

data$hour <- date.to.hour(as.POSIXct(data[,1], origin="1970-01-01"))
+1

. :

aggregate(. ~ cut(time, 'hours'), data, mean)

If you only need a quick chart, ggplot2 is your friend:

qplot(cut(time, "hours"), count, data=data, stat='summary', fun.y='mean')

Unfortunately, since cut returns a coefficient, the x axis will not work properly. You might want to write your own, less inconvenient bucketing function for time, for example.

timebucket = function(x, bucketsize = 1,
                      units = c("secs", "mins",  "hours", "days", "weeks")) {
  secs = as.numeric(as.difftime(bucketsize, units=units[1]), units="secs")
  structure(floor(as.numeric(x) / secs) * secs, class=c('POSIXt','POSIXct'))
}
qplot(timebucket(time, units="hours"), ...)
+1
source

All Articles