-1

Hi I want to take the average of the following dataset:

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

Desired output:

Chapter_number  Average_of_time
4                   8.5
5                   ...
6                   ...
7                   ...

Thanks in advance

Preet Rajdeo
  • 377
  • 1
  • 5
  • 11
  • 3
    And... what have you tried already? – Peter Badida Apr 04 '16 at 21:55
  • 5
    take a look at the `aggregate` function in R. – fishtank Apr 04 '16 at 21:57
  • 1
    Googling for even unspecific search terms like `r average data` turns up multiple Stackoverflow links answering this question. Typing `r average...` also auto-suggests `r average by group` as well. Hence the downvote for absolutely no research effort. – thelatemail Apr 04 '16 at 22:52

2 Answers2

1

My understanding of aggregate is limited, this is how I would go about it. There ARE better ways, this is simply mine.

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
Badger
  • 1,043
  • 10
  • 25
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
bgoldst
  • 34,190
  • 6
  • 38
  • 64