rtime-seriesfinancial

R: finding percentages with time series data


I am working with financial data on R and I need to know what is the percentage of days with a stock return larger than 5% in absolute value.

I have the variable "returns" and my intuition is that I should create another variable for which the absolute value of the return is larger than 5% and then see how many elements would that variable vector contain and compute the ratio between its length and the total number of observations. However, I need help with the commands I should use on R.

(I know this may sound very easy but I am not used to analysing financial data with R)


Solution

  • If you just want a number and returns is a numeric vector, you could try

    sum(abs(returns) > 0.05) / length(returns)
    

    where we are counting the number of absolute returns greater than 5% in the numerator and dividing by the number of observations, as you described. Try using ?sum, ?abs, ?length for more documentation on what these functions do.