0

I'm sure something similar has been asked, but I'm kind of new to R and can not find something that works. I'm using the forecast package to create a moving average from a year's worth of data. I would like run this function:

CityColl$cnt_ma <- ma(CityColl$clean_cnt, order=7)

But separately for each city.

I've tried split and apply functions something like this:

CityColl$cnt_ma <- ddply(CityColl, .(city), ma(CityColl$clean_cnt, order=7))

But this seems to be very wrong...

       start_time       city clean_cnt
   <date>      <chr>        <int>
     1 2016-01-01     Boston          242
     2 2016-01-01    Chicago          935
     3 2016-01-01 Washington         3220
     4 2016-01-01     Philly          639
     5 2016-01-02     Boston          221
     6 2016-01-02    Chicago         1421
     7 2016-01-02 Washington         4922
     8 2016-01-02     Philly          549
     9 2016-01-03     Boston          245
     10 2016-01-03    Chicago         1399
Frank
  • 66,179
  • 8
  • 96
  • 180
R. Buote
  • 1
  • 2
  • 2
    Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete, and verifiable example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also include the names of any packages that you are using. `ma` is not a base R function. – lmo Aug 23 '17 at 16:21
  • The example in the duplicate can perfectly be applied here: `CityColl$MA <- ave(CityColl$clean_cnt,CityColl$city,FUN=function(v)as.numeric(ma(v,order=7)))` – digEmAll Aug 23 '17 at 17:55
  • That's perfect! Thank you so much for your help and feedback. – R. Buote Aug 24 '17 at 13:38

1 Answers1

0

Try this:

require(data.table)
require(forecast)

setDT(CityColl)

CityColl[, ma(city, order = 7), by = city]
Mako212
  • 6,787
  • 1
  • 18
  • 37
  • Thank you for the response, this produces an error "Warning in storage.mode(x) <- "double" : NAs introduced by coercion Error in filter(x, w) : 'filter' is longer than time series" – R. Buote Aug 23 '17 at 16:40
  • @richardb: that's because you're using order=7 with a series of 2 or 3 values (yes, when you split by city you get only 2/3 values...) – digEmAll Aug 23 '17 at 16:52
  • The data I have is for a year, I simply provided a small sample of what the data looks like. Having never posted a question before, is there a better way to show this? – R. Buote Aug 23 '17 at 16:54
  • @richardb: your example is ok, maybe you can change order (e.g. order=3) and reduce the number of different cities to increase the series for each value, but your question is clear. – digEmAll Aug 23 '17 at 17:44