I have price data indexed according to three things:
Status, date and UPC (this is the product code).
I have a bunch of prices that are equal to NA.
I try to fill in the NS as follows: for a given missing price with an index (S, D, UPC) fill in the average price of all data points with the same S and UPC. Ie, take the average value by date.
There must be an incredibly easy way to do this, because it is very simple. I use for loops, but now I understand that it is incredibly inefficient, and I would like to use a function, for example, one in plyr or dplyr, which will do all this as few steps as possible.
upc=c(1153801013,1153801013,1153801013,1153801013,1153801013,1153801013,2105900750,2105900750,2105900750,2105900750,2105900750,2173300001,2173300001,2173300001,2173300001)
date=c(200601,200602,200603,200604,200601,200602,200601,200602,200603,200601,200602,200603,200604,200605,200606)
price=c(26,28,NA,NA,23,24,85,84,NA,81,78,24,19,98,NA)
state=c(1,1,1,1,2,2,1,1,2,2,2,1,1,1,1)
data <- data.frame(upc,date,state,price)
price=c(26,28,27,27,23,24,85,84,79.5,81,78,24,19,98,47)
data2 <- data.frame(upc,date,state,price)
Any tips? Thanks.