我有以下数据框:
library("lubridate")
df = data.frame(c("AAA","BBB","AAA"),
c("2012","2013","2012"),
c("2012-12-30 08:01:01","2013-12-30 09:05:02","2012-12-30 08:08:01"),
c("2012-12-30 09:01:00","2013-12-30 10:15:00","2012-12-30 08:11:01"))
colnames(df) = c("type","year","start","end")
df$duration = difftime(ymd_hms(df$end), ymd_hms(df$start),units="mins")
现在我想创建一个具有平均持续时间的表,以便它看起来如下:
2012 2013
AAA 31.49 0.00
BBB 0.00 69.97
我应该使用哪些函数来创建这样的表?
更新
这是我尝试过的,但如何添加year
?:
mean_duration_per_type_year = aggregate(duration~type,
data=df,
mean)
答案 0 :(得分:0)
使用reshape2
包
library(reshape2)
temp <- aggregate(df$duration, by = list(type = df$type, year = df$year), mean)
final <- dcast(temp, type~year)
final[is.na(final)] <- 0 # optional : replace the NA values with 0
final
# type 2012 2013
# 1 AAA 31.49167 0.00000
# 2 BBB 0.00000 69.96667
请注意,如果您希望类型为rownames,则可以添加以下行:
rownames(final) <- final$type
final$type <- NULL
final
# 2012 2013
# AAA 31.49167 0.00000
# BBB 0.00000 69.96667