ageil
ageil

Reputation: 171

R: Summing POSIXct-objects using tapply

I'm trying to sum a set of POSIXct objects by a factor variable, but am getting an error that sum is not defined for POSIXt objects. However, it works fine if I just calculate the mean. But how can I get the summed times by group using tapply?

Example:

data <- data.frame(time = c("2:50:04", "1:24:10", "3:10:43", "1:44:26", "2:10:19", "3:01:04"),
                   group = c("A","A","A","B","B","B"))
data$group <- as.factor(data$group)
data$time <- as.POSIXct(paste("1970-01-01", data$time), format="%Y-%m-%d %H:%M:%S", tz="GMT")

# works
tapply(data$time, data$group, mean)

# doesn't work
tapply(data$time, data$group, sum)

Upvotes: 1

Views: 4379

Answers (1)

Patrick Roocks
Patrick Roocks

Reputation: 3259

Date objects cannot be summed, this does semantically not make sense, the + operator is also not defined for POSIXct objects.

Probably you want to model time differences and sum them up?

Try:

times <- as.difftime(c("2:50:04", "1:24:10", "3:10:43", 
                       "1:44:26", "2:10:19", "3:01:04"), "%H:%M:%S")
sum(times)

A difftime object is also that what you get when you subtract two date objects (which is semantically reasonable).

EDIT:

A entire solution for the OPs problem in a semantically neater way (tapply seams to destroy the structure of the difftime class - use group_by from the dplyr package instead)

library(dplyr)

times <- as.difftime(c("2:50:04", "1:24:10", "3:10:43", 
                       "1:44:26", "2:10:19", "3:01:04"), format="%H:%M:%S")

data <- data.frame(time = times, group = c("A","A","A","B","B","B"))

summarise(group_by(data, group), sum(time))

This gives the following output:

Source: local data frame [2 x 2]

  group      sum(time)
1     A 7.415833 hours
2     B 6.930278 hours

Upvotes: 1

Related Questions