How to take the variation in relation to the number of days of the difference of dates in the R

1

I have a table in csv with 5 information, two with dates, two with prices, and one with the difference between dates.

  • I have the date 1 set to a date and the date 2 varies.

For example:

10/01/2018 > 11/01/2018
10/01/2018 > 1/12/2018 ...

  • I have the percentage of variation that I got through the script below:

Tipo1_X <- (sd(Tipo1$precoin, na.rm = T)/mean(Tipo1$precoin, na.rm = T))*100 Tipo2_X <- (sd(Tipo2$precoin, na.rm = T)/mean(Tipo2$precoin, na.rm = T))*100 Tipo3_X <- (sd(Tipo3$precoin, na.rm = T)/mean(Tipo3$precoin, na.rm = T))*100 Tipo1_XX <- (sd(Tipo1$precoout, na.rm = T)/mean(Tipo1$precoout, na.rm = T))*100 Tipo2_XX <- (sd(Tipo2$precoout, na.rm = T)/mean(Tipo2$precoout, na.rm = T))*100 Tipo3_XX <- (sd(Tipo3$precoout, na.rm = T)/mean(Tipo3$precoout, na.rm = T))*100

  • Now, I need to know the variation of days in relation to this price. For example:

I have from 10/01/2018 to 11/01/2018, after the 10/01/2018 to the 12/01/2018 .. From the 10th to the 11th is a day difference of the day 10 to day 12 is 2 days. I need to get the variation on the number of days of the difference, but I can not find a way to do this in R.: (

PS: Exemplifying what the result would look like:

In differences of dates of 2 days the price varies by 10%, in 30-day date differences the price varies by 5%, and so on.

    
asked by anonymous 10.07.2018 / 21:14

1 answer

0

I'm not sure the question is very clear and maybe it should be a bit of additional information. Either way, assuming you always want to calculate the difference versus 10-01-2018, then you need to use indexes. You can calculate the results you are looking for as follows:

Diff_2_dias <- (valor_12012018/valor_100120118 -1) * 100
Diff_30_dias <- (valor_09022018/valor_100120118 -1) * 100

Note : The variables are exemplary, you have to choose the values according to your dataframe and you can use the base R without needing any packages.

Since you are handling serious temps, I advise you to take a look at the XTS package documentation and convert the series into a time series. It will save you a lot of time in data manipulation.

link

link

    
11.07.2018 / 10:19