我有以下数据帧:
DF<-data.frame(stringsAsFactors = TRUE,
Sample = c(rep("s1",4),rep("s2",4)),
date = c("21/07/2020","24/07/2020","25/07/2020","27/07/2020",
"03/08/2020","06/08/2020","09/08/2020","10/08/2020"))
首先,我想通过因子"0"来获得连续日期之间的天数;样品";。所以输出是这样的:
DF_2<-data.frame(stringsAsFactors = TRUE,
Sample = c(rep("s1",4),rep("s2",4)),
date = c("21/07/2020","24/07/2020","25/07/2020","27/07/2020",
"03/08/2020","06/08/2020","09/08/2020","10/08/2020"),
days = c(NA,3,1,2,NA,3,3,1))
其中变量";天";是我的结果变量。之后,我想添加所有这些";天";按因素。但这很容易,会这样做:
df_3<-aggregate(days~Sample,DF_2,sum)
如果有人帮我迈出正确的第一步,得到DF_2,我将不胜感激。
我们可以使用diff
来获得Date
类转换的"日期"列之间的差异
library(dplyr)
library(lubridate)
DF1 <- DF %>%
mutate(date = dmy(date)) %>%
group_by(Sample) %>%
mutate(days = c(NA, diff(date))) %>%
ungroup