I'm new to the R
language and I'm having some difficult to calculate the returns of my dataset for every Identification
.
I have a very large dataset of monthly observations grouped like so:
Code Subset Identification Names Times Value %
100 1001 10011 ..... 201012 10 40
100 1001 10012 ..... 201012 11 60
100 1002 10021 ..... 201012 7 30
100 1002 10022 ..... 201012 13 70
.....
100 1001 10011 ..... 201301 11 45
100 1001 10012 ..... 201301 15 55
100 1002 10021 ..... 201301 9 33
100 1002 10022 ..... 201301 17 67
I need to write a function that can calculate the monthly rate of returns for every Identification
. Then, I need to aggregate the values so calculated in the upper level of "subset" (with a mean weighted "%").
I've changed the format of the vector times
to year-month
i.e. "%Y-%m" in this way:
as.yearmon(as.character(Data$Times), format = "%Y%m")
and I've tried to calculate the returns for every Identification
using split
and sapply
, like this:
xm <- split(Data, Identification)
Retxm <- sapply(1:length(xm), function(x) returns(Value))
The output i had using the function above is like this:
[,1] [,2] [,3] [,4]
[1,] NA NA NA NA
[2,] 1.605198e-03 1.605198e-03 1.605198e-03 1.605198e-03
[3,] -1.190902e-02 -1.190902e-02 -1.190902e-02 -1.190902e-02
[4,] 3.318032e-03 3.318032e-03 3.318032e-03 3.318032e-03
The output is not many clear, so i would have on the row the Times and on the header the Identification
.
Thank you so much!