I have a data frame with an ID column, a date column (12 months for each ID), and I have 23 numeric variables. I would like to obtain the percentage change by month within each ID. I am using the quantmod package in order to obtain the percent change.
Here is an example with only three columns (for simplicity):
ID Date V1 V2 V3
1 Jan 2 3 5
1 Feb 3 4 6
1 Mar 7 8 9
2 Jan 1 1 1
2 Feb 2 3 4
2 Mar 7 8 8
I tried to use dplyr and the summarise_each function, but that was unsuccessful. More specifically, I tried the following (train is the name of the data set):
library(dplyr)
library(quantmod)
group1<-group_by(train,EXAMID)
foo<-function(x){
return(Delt(x))
}
summarise_each(group1,funs(foo))
I also tried to use the do function in dplyr, but I was not successful with that either (having a bad night I guess!).
I think that the issue is the Delt function. When I replace Delt with the sum function:
foo<-function(x){
return(sum(x))
}
summarise_each(group1,funs(foo))
The result is that every variable is summed across the date for each ID. So how can about the percentage change month-over-month for each ID?