-1

Below is my data frame named data:

slot  id  time  number

  1     1     0     6
  1     1     1     8
  1     1     2     3
  1     1     3     6
  1     1     4     8
  1     1     5     3
....
  1     1     23    9
  1     2      0    6
  1     2      1    9
  1     2      2    3
  1     2      3    6
  1     2      4    9
  1     2      5    6
  1     2      6    9
  1     2      7    6
  1     2      8    3
....
  1     2      23   3

....


  2     1     0     8
  2     1     1     3
  2     1     2     8
  2     1     3     3

.....
  2     1     3     3
 .....


 30    80    23     3

Likewise I have 30 slots and 82 id and 23 time(0-23).So I want to take the average for each id . Initially filter one id:

 slot  id  time  number

  1     1     0     6
  1     1     1     8
  1     1     2     3
  1     1     3     6
  1     1     4     8
  1     1     5     3
....
  1     1     23    9
  2     1     0     8
  2     1     1     3
  2     1     2     8
  2     1     3     3
.....
 30    1     23    3

So I would like to take average of number for the above id which means add all the number and divide by 720 (24*30). average=sum(data$number)/720

So for one id I can easily do this But how to do this for all the id's at a time and store as a data frame like:

id     average
___    _______
 1       **
...
 80      **

Any help is appreciated

John
  • 305
  • 2
  • 4
  • 18

1 Answers1

1
df %>%
   group_by(id) %>%
   summarise(average = mean(number))
jenswirf
  • 7,087
  • 11
  • 45
  • 65
  • 2
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Feb 15 '17 at 13:49