I'm attempting to calculate percent change over a period of time where there are certain days that do not have any data. The problem I am running into is that the Delt function produces NA's (I'm guessing because I have missing dates?)
The code looks something like this:
Date <- c("6/1/2015", "6/3/2015", "6/4/2015", "6/5/2015", "6/8/2015")
variable <- c(4,7,10,22,3)
df <- data.frame(Date, variable)
df$Date <- as.Date(df$Date, "%m/%d/%Y")
df
library(plyr)
library(quantmod)
perct.Change <- ddply(df, "Date", transform,
Daily.Change = round(Delt(variable)*100,1))
and the data looks like this:
Date variable Delt.1.arithmetic
1 2015-06-01 4 NA
2 2015-06-03 7 NA
3 2015-06-04 10 NA
4 2015-06-05 22 NA
5 2015-06-08 3 NA
I think the
ddply
function isn't working like you think it is - it is applyingDelt
to a dataframe at each time point and returningNA
, as that is always the first output ofDelt
in a time series.You probably want something like this:
Where we have calculated the Delt, then divided it by the difference in days between measures.