【发布时间】:2018-10-02 15:18:33
【问题描述】:
假设我们有这些数据:
type <- paste("type", c(1,1,1,2,3,1,2,2,3,3,3,3,1,1))
dates <- seq(as.Date("2000/1/1"), by = "days", length.out = length(type))
mydataframe <- data.frame(type, dates)
我在其他posts 中看到rle 可能会完成这项工作,但我想获得一个数据框,对于每种类型,我的平均持久性都在天数内。比如:
> print(persistance)
type1 type2 type3
1 2 1.5 2.5
请问有人知道怎么做吗? 谢谢!
【问题讨论】:
-
runs <- rle(mydataframe$type); aggregate(lengths ~ values, unclass(runs), mean) -
感谢您的帮助!
标签: r count duration run-length-encoding