Applying rollify to calculate percentrank

37 Views Asked by At

I have the table Wide_DF similar to the below structure( more rows than below)

DATE TICKER Total_Put
2021-01-03 A 23.4
2021-01-03 B 13.1
2021-01-03 C 34.6

The colum TICKER has 3 values A,B,C, i.e the data for each ticker for all dates, and dates in descending order

My goal is to have a 4th column explaining the percentile of Total_Put with the last 60 values

This is my approach

rankCalc <- tibbletime::rollify( ~percent_rank(.x), window = 60)



Wide_DF = Wide_DF %>%
          dplyr::group_by(TICKER) %>%
          dplyr::mutate(ROW_VALUE = rankCalc(Total_Put )) %>%
          dplyr::ungroup()

But I get the following error

Error in dplyr::mutate():
ℹ In argument: ROW_VALUE=rankCalc(Total_Put ). ℹ In group 1: TICKER = "A".
Caused by error: ! ROW_VALUE must be size 1060 or 1, not 60119.

It looks like an error due to the window size, but I verified the window size is appropriate by replacing rank with mean and the code works. Even my Wide_DF has 3000 rows

What looks like the possible error ?

0

There are 0 best solutions below