0

I have time as my variable v1 and occupancy rate as my v2.

   Interval    Percentage
    18:00:00   100%
    18:00:00    99%
    18:00:00    98%
    18:00:00   100%
    18:30:00    89%
    18:30:00    88%
    18:30:00    90%
    19:00:00    93%
    19:00:00    92%
    19:00:00    93%

Say I want to take the average of occupancy for interval 18:00:00, how am I supposed to do with R? I tried the following:

library(plyr)
ddply(B, .(B$Interval), summarize, mean_y=mean(B$Percentage)) 

   B$Interval     mean_y 
1    18:30:00  0.6795594 
2    18:35:00  0.6795594 
3    18:40:00  0.6795594
4    18:45:00  0.6795594
5    18:50:00  0.6795594
6    18:55:00  0.6795594
7    19:00:00  0.6795594
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
Siewmei Loh
  • 45
  • 1
  • 8
  • 2
    What did you try so far? Did you search here for similar problem? –  Oct 21 '15 at 03:00
  • I have tried using plyr func. but the avg value turned out to be repetitive. > library(plyr) > ddply(B, .(B$Interval), summarize, mean_y=mean(B$Percentage)) B$Interval mean_y 1 18:30:00 0.6795594 2 18:35:00 0.6795594 3 18:40:00 0.6795594 4 18:45:00 0.6795594 5 18:50:00 0.6795594 6 18:55:00 0.6795594 7 19:00:00 0.6795594 – Siewmei Loh Oct 21 '15 at 03:24
  • 1
    It appears that the `B` you used in your `plyr` code is not the example data you included in your question. Please update your question by providing `B`, possibly using the `dput` function. You can read more about producing reproducible examples at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – josliber Oct 21 '15 at 03:29
  • 1
    You don't need `B$` inside `ddply` – akrun Oct 21 '15 at 03:50
  • 1
    As akrun says, skip all the `B$` inside ddply and it will work. `mean(B$Percentage)` just calculates the overall mean of the Percentage column in all of B. `mean(Percentage)`, when used with `summarize` inside `ddply`, calculates the mean percentage for each subset of `B` with the given interval. – mathematical.coffee Oct 21 '15 at 04:09
  • 1
    @akrun and mathemactical.coffee answers work! :) – Siewmei Loh Oct 21 '15 at 07:08

0 Answers0