0

I've tried to calculate the media of a data frame given a condition and generate a entry of a time series for every media calculate. This is an example of how my datatable looks like:

month        price        dept
01-2013      872          A
01-2013      875          B
01-2013      872          A
01-2013      973          C
02-2013      853          A
02-2013      986          A
02-2013      953          B 
02-2013      743          C

Where the date 02-2013 represents month=february, year=2013. I would like to generate a time series of the mean of the price for every month from January of 2013 to October of 2019. I´ve tried to make a function which depends of the month and the year.

Here´s what I've attempted:

 meses <-c("01","02","03","04","05","06","07","08","09","10","11","12")
    años <- c("2013","2014","2015","2016","2017","2018","2019")
    
   promedio <- function(m,a){
   for(i in  m){
        for (j in a){
           mean(tabla3$price[tabla3$month ==i-j],na.rm = T) 
          }
                }
   }

But when I try to test it with the line

promedio(01,2013)

It doesn't give any answer.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Valeria Arango
  • 287
  • 1
  • 2
  • 8
  • Hi Valeria. Welcome to SO. First. It's not clear what you are trying to achieve and what's exactly your issue. Maybe you should have a look at [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). However, from your code I would guess that `for(i in meses)` and `for (j in años)` should be `for (j in m)` and `for (j in a)` as m and a are the names of your function arguments. Additonally, `tabla3$month == "i-j"` should probably be `tabla3$month == i-j`. – stefan Oct 06 '20 at 21:52
  • I'm gonna edit my question... – Valeria Arango Oct 07 '20 at 19:48

0 Answers0