Tyu1990 Tyu1990 - 21 days ago 10
R Question

R: rolling frequency

Hi I have to account the rolling frequency.

better explain:

i have a data frame like this

Date ID IDC
30/12/2015 A D
30/10/2015 A E
30/9/2015 A D
30/7/2015 A F
30/6/2016 A F
30/12/2015 B G
30/10/2015 B D
30/9/2015 B G
30/7/2015 B E
30/6/2016 B G


i have to account the rolling frequency IDC (3 month) by ID

The final Data frame should be:

Date ID IDC Freq
30/12/2015 A D 2
30/12/2015 A E 1
30/10/2015 A E 1
30/10/2015 A D 1
30/10/2015 A F 1
30/9/2015 A D 1
30/9/2015 A F 1
...
30/12/2015 B G 2
30/12/2015 B D 1
30/10/2015 B D 1
30/10/2015 B D 1
30/10/2015 B E 1
30/9/2015 B G 2
30/9/2015 B E 1


how can i do it?

Answer

Using the data.table package and assuming df is your dataframe:

library(data.table)
setDT(df)[, freq:= .N, by = c("ID","IDC")]
Comments