I am trying to stop using for loops when I code but I have a bit of a problem representing a simple operation.
Let's say I am trying to do simple nearest-neighbour estimation on a dataset for a company that owns several restaurants. I have three features: City, Store, Month and one target function Sales. City,Store and Month are all represented with numbers: City takes values between 1-100, Store takes values between 1-50 and Month between 1-12.
Now, I want to replace this for-loop with an apply function:
for (c in 1:100){
for (s in 1:50){
for (m in 1:12){
dat1$Sales[dat1$City==c & dat1$Store==s & dat1$Month==m & is.na(dat1$Sales)] <-
mean(dat1$Sales[dat1$City==c & dat1$Store==s & dat1$Month==m & !is.na(dat1$Sales)])
}
}
}
What is the complexity of this apply function?
Many thanks!