I have a dataset that has date and another variable (bank rate). Here is a snippet of the data:
I want to calculate the average number of days between each consecutive change in Bank rate. For example to get an output as such:
Essentially, I am trying to calculate the average number of days a rate remains for before it changes.
I am able to do the usual difftime() function, However I need it to specifically only calculate the difference when there are changes in the rate, and then average it out. I am new to R and unable to figure out how to go about this


I have a made a random sequence of dates in the timeframe as above and have used
bank_ratefrom above and put them in a DF.This DF is ordered for
date. Data which do not show any change inbank_rateare then removed byfilter. (see consecutivebank_rates of 2). A new variabledays_from_beforeis created which calculates the number of days of consecutive dates.The average is calculated as the
meanfromdays_from_before.