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?
The issue you are running into is because your data is not formatted in a "tidy" way. You have observations (V1:V3) that are in columns creating a "wide" data frame. The "tidyverse" works best with long format. The good news is with the gather()
function you can get exactly what you need. Here's a solution using the "tidyverse".
library(tidyverse)
# Recreate data set
df <- tribble(
~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
)
df
#> # A tibble: 6 × 5
#> ID Date V1 V2 V3
#> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 1 Jan 2 3 5
#> 2 1 Feb 3 4 6
#> 3 1 Mar 7 8 9
#> 4 2 Jan 1 1 1
#> 5 2 Feb 2 3 4
#> 6 2 Mar 7 8 8
# Gather and calculate percent change
df %>%
gather(key = key, value = value, V1:V3) %>%
group_by(ID, key) %>%
mutate(lag = lag(value)) %>%
mutate(pct.change = (value - lag) / lag)
#> Source: local data frame [18 x 6]
#> Groups: ID, key [6]
#>
#> ID Date key value lag pct.change
#> <dbl> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 1 Jan V1 2 NA NA
#> 2 1 Feb V1 3 2 0.5000000
#> 3 1 Mar V1 7 3 1.3333333
#> 4 2 Jan V1 1 NA NA
#> 5 2 Feb V1 2 1 1.0000000
#> 6 2 Mar V1 7 2 2.5000000
#> 7 1 Jan V2 3 NA NA
#> 8 1 Feb V2 4 3 0.3333333
#> 9 1 Mar V2 8 4 1.0000000
#> 10 2 Jan V2 1 NA NA
#> 11 2 Feb V2 3 1 2.0000000
#> 12 2 Mar V2 8 3 1.6666667
#> 13 1 Jan V3 5 NA NA
#> 14 1 Feb V3 6 5 0.2000000
#> 15 1 Mar V3 9 6 0.5000000
#> 16 2 Jan V3 1 NA NA
#> 17 2 Feb V3 4 1 3.0000000
#> 18 2 Mar V3 8 4 1.0000000