您好我想取以下数据集的平均值:
visitor_id
Chapter_number Time
1 4 8
2 4 9
3 5 1
4 5 2
5 6 3
6 6 4
7 6 5
8 6 6
9 7 7
10 7 8
期望的输出:
Chapter_number Average_of_time
4 8.5
5 ...
6 ...
7 ...
提前致谢
答案 0 :(得分:1)
我对聚合的理解是有限的,我就是这样做的。有更好的方法,这只是我的。
x <- data.frame(chapter_number = rep(seq(4,7,1),3),time=seq(1,36,3))
x$meantime <- aggregate(x , by = list( Chapter = x$chapter_number ),FUN = "mean" )$time
x
chapter_number time meantime
4 1 13
5 4 16
6 7 19
7 10 22
4 13 13
5 16 16
6 19 19
7 22 22
4 25 13
5 28 16
6 31 19
7 34 22
答案 1 :(得分:0)
visitor_id <- data.frame(Chapter_number=c(4L,4L,5L,5L,6L,6L,6L,6L,7L,7L),Time=c(8L,9L,1L,2L,3L,4L,5L,6L,7L,8L));
aggregate(cbind(Average_of_time=Time)~Chapter_number,visitor_id,mean);
## Chapter_number Average_of_time
## 1 4 8.5
## 2 5 1.5
## 3 6 4.5
## 4 7 7.5