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.