如何按组(在R,ggplot,dplyr,tidyverse中)用R编写更简单的派生? [英] How a simpler derive is written in R by group (in R, ggplot, dplyr, tidyverse)?
问题描述
在同一列中,我在y轴上有两个数据,在x轴上有一个日期时间.
我尝试为每两个数据计算数值导数,但我不理解R中的导数.(我正在寻找 stats :: D
或 diff
但这是行不通的.
f(x)=(t_n-t_n-1)/(date_time_n/date_time_n -1)
其中f(x)是我的计算列.
ie用执行此操作的函数替换下面代码中的 calc = t/10
.(我更喜欢tidyverse/dplyr)
链接
-
库(tidyverse)库(ggplot2)数据< -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个))数据<-(数据%>%变异(calc = t/10))(ggplot(数据)+ 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(映射= aes(x = date_heure,y =计算,color = orig),形状= 21,填充=白色")+ 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")))))
解决方案据我了解,您希望使用当前和以前的
t
和date_heure
值.要获取特定列中前一行的值,可以使用lag
,如下所示:datas<-(数据%>%变异(calc =(t-lag(t))/as.integer((date_heure-lag(date_heure)))))
请注意,第一行的
calc
值将为NA
.因此,在绘制图形之前,可能需要跳过并为其提供默认值.例如:
datas<-数据[-1,]#跳过第一个`NA`值datas [1,] $ calc<-0#为它提供默认值'0'
希望它会有所帮助.
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
ordiff
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
- Calculate the derivative of a data-function in r
- My previous question To add a legend by shape in ggplot ( added 2nd question after: to calculate numeric deriv)
Below : ggplot picture of
calc=t/10
wherecalc
will be replaced by the derive.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")))) )
解决方案As I understood, you want
calc
to be computed using the current and previoust
anddate_heure
values. To get the value of a previous row in a particular column, you can uselag
, 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 beNA
. 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.
这篇关于如何按组(在R,ggplot,dplyr,tidyverse中)用R编写更简单的派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!