1

I have two datas in one column by their name in y axis, and a datetime in x axis.

I try to calculate numeric deriv for each two datas, but I don't understand derive in R. (I've looking for stats::D or diff but It doesn't work).

f(x)=(t_n-t_n-1)/(date_time_n / date_time_n -1)

where f(x) will be my calc column.

ie to replace my calc=t/10, in the code below, by a function doing this. (I would prefer tidyverse / dplyr)

Links

Below : ggplot picture of calc=t/10 where calc will be replaced by the derive.

enter image description here

library(tidyverse)
library(ggplot2)

datas<-data.frame(
  t = c(
    50 + c(0, cumsum(runif(9, -7, 7))),
    70 + c(0, cumsum(runif(9, -10, 10)))
  ),
  orig=c(rep("s1",10),rep("s2",10)),
  date_heure = rep(
    seq(from=as.POSIXct("2012-1-1 0:00", tz="UTC"),by="hour", length=10) ,
    2
  ) 
)


datas<- (datas 
         %>% mutate (
           calc=t/10
         )
)


(
  ggplot(datas) 
  +   geom_line(mapping=aes(x = date_heure, y = t, color=orig, linetype = "s1"))
  +   geom_line(mapping=aes(x = date_heure, y = calc, color=orig, linetype = "s2"))
  +   scale_y_continuous(name = "t", sec.axis = sec_axis(trans=~(range(datas$calc)), name = "calc"))
  +   geom_point(mapping = aes(x = date_heure, y = calc, color=orig), shape = 21, fill = "white")
  +   scale_color_manual(name = "calc", values=c("red", "blue"))
  +   scale_linetype_manual(name = "orig", values = c('solid', 'solid'), 
                            guide = guide_legend(override.aes = list(colour=c("red", "blue"))))

)
phili_b
  • 885
  • 9
  • 27

2 Answers2

1

As I understood, you want calc to be computed using the current and previous t and date_heure values. To get the value of a previous row in a particular column, you can use lag, as follows:

datas<- (datas
         %>% mutate (
           calc = (t - lag(t)) / as.integer((date_heure - lag(date_heure)))
        )
)

Please note that the value of calc for the first row is going to be NA. Hence, you may need to skip and give it a default value before you plot your figure.

For example:

datas <- datas[-1,]  # To skip the first `NA` value
datas[1,]$calc <- 0  # To give it a default value of `0`

Hope it helps.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
  • Thank you. It works and the datas and the plot seems like my user talk about it. I will accept as soon as my user have seen it in some days (~Tuesday ). – phili_b Sep 20 '19 at 15:54
  • Glad to hear that it worked for you. You may give my answer an upvote for now and then accept it later :-) – Taher A. Ghaleb Sep 20 '19 at 17:14
  • Thank you. It seems ok...I validate. (I've deleted the comments about ggplot and plotly not accurate for this questions). – phili_b Sep 24 '19 at 19:10
0

The following lines will add a new row with the lagged time values per group.

library(dplyr)
data <- 
    data %>%
    group_by(groups) %>%
    mutate(lag.value = dplyr::lag(value, n = 1, default = NA))

Similarly, you could add another column where you calculate the 1st (forward) difference coefficient by a formula of your choice. Please note that if you have NA values, things might become more complicated.

More explanations and alternative approaches you find at How to create a lag variable within each group?

B--rian
  • 5,578
  • 10
  • 38
  • 89
  • Thank you but could you modify your code with my variable if you want I accept your answer. I tried `datas <-( datas %>% group_by(orig) %>% mutate(calc = dplyr::lag(t, n = 1, default = NA)))`, it displays datas but ggplot doesn't work. – phili_b Sep 20 '19 at 15:44
  • To accept your answer I have also to wait the answer of my user in some days. – phili_b Sep 20 '19 at 15:46
  • Thanks for the link. – phili_b Sep 20 '19 at 15:55