I am using RStudio to write a script that simply populates a new column in a data.frame based upon a simple formula involving another column in the same data.frame. I eventually want to add an internal loop, which is why I think a for loop is the way to go. I am new to r so I am not sure. At some point in the development of the user defined function, BD, PS and LT (see below) will be arguments in a user defined function. I created a very simple data.frame for illustration. The question is: How do I get this loop or some formula that will populate the values of media_stock with the simple exponential decay formula in the loop?
TV = data.frame(cbind(cable = c(215, 144, 66, 400, 0, 0, 0, 0, 0, 0, 0, 500, 0, 0, 0, 0, 0, 77, 99, 108, 86, 118, 204, 119, 144, 0, 0, 0, 450, 361, 438, 170, 244, 0, 0, 0, 0, 0, 107, 122, 207, 274, 0, 0, 0, 0, 0, 0, 0, 752, 700,665),media_stock = NA))
PS = 0.30
BD = 0
LT = 6
for(i in (LT+1):length(TV)){
TV$media_stock[i-BD] <- TV$cable[i-BD] + PS*TV$media_stock[i-BD-1]
}
Thanks for any advice