在R中递增地添加按ID分组的时间戳列的秒数

在R中递增地添加按ID分组的时间戳列的秒数,r,ggplot2,time-series,R,Ggplot2,Time Series,我有一个基本上是时间序列数据的数据帧 Timestamp <- c("1/27/2015 18:28:16","1/27/2015 18:28:17","1/27/2015 18:28:19","1/27/2015 18:28:20","1/27/2015 18:28:23","1/28/2015 22:43:08","1/28/2015 22:43:09","1/28/2015 22:43:13","1/28/2015 22:43:15","1/28/2015 22:43:16"

我有一个基本上是时间序列数据的数据帧

    Timestamp <- c("1/27/2015 18:28:16","1/27/2015 18:28:17","1/27/2015 18:28:19","1/27/2015 18:28:20","1/27/2015 18:28:23","1/28/2015 22:43:08","1/28/2015 22:43:09","1/28/2015 22:43:13","1/28/2015 22:43:15","1/28/2015 22:43:16"
) 
    ID <- c("A","A","A","A","A","B","B","B","B","B")
    v1<- c(1.70,1.71,1.77,1.79,1.63,7.20,7.26,7.16,7.18,7.18) 
    df <- data.frame(Timestamp ,ID,v1)

Timestamp            ID             v1
1/27/2015 18:28:16   A              1.70
1/27/2015 18:28:17   A              1.71
1/27/2015 18:28:19   A              1.77
1/27/2015 18:28:20   A              1.79
1/27/2015 18:28:23   A              1.63
1/28/2015 22:43:08   B              7.20
1/28/2015 22:43:09   B              7.26
1/28/2015 22:43:13   B              7.16
1/28/2015 22:43:15   B              7.18
1/28/2015 22:43:16   B              7.18
我还想用ggplot绘制这个图,用ID表示间隔vs v1,所以我们在同一个图中得到了2个时间序列。然后我将从中提取特征


请帮助我如何解决这个问题,以便我可以将其应用于更大的数据集

一个包含
数据的解决方案。表

有关数据:

library(data.table)
df <- as.data.table(df)
df$Timestamp <- as.POSIXct(df$Timestamp, format='%m/%d/%Y %H:%M:%S')
df[, Interval := as.numeric(difftime(Timestamp, .SD[1, Timestamp], units='secs') + 1)   , by=ID]
然后对于
ggplot

library(ggplot2)
ggplot(df, aes(x=Interval, y=v1, color=ID)) + geom_line()
图中:


哇,这正是我需要的。我在我的大数据集中运行了你的代码,它工作得很好。非常感谢你。我只需要再多一点帮助。我在更大的数据集中得到了ID的重叠图。是否有一种方法可以将它们彼此相邻放置在窗口或ggplot中的各个部分(按ID)?如果我不清楚我想要什么,请告诉我。我还根据你所说的对我的输出表进行了编辑。非常欢迎:)。很高兴我能帮上忙!当然,你能做到。只需使用以下命令:
ggplot(df,aes(x=Interval,y=v1,color=ID))+geom\u line()+facet\u grid(.~ID)
,它们将被绘制为面,即在部分中。您可以看到关于面的更多信息!!。。精彩而美丽的情节!!!男人。。你为我节省了很多时间。:)我真的很感激。我将把这项工作扩展到许多变量,并对其进行处理。它们确实是
ggplot2
是一个很棒的库!不客气。我真的很高兴能帮上忙:)
library(data.table)
df <- as.data.table(df)
df$Timestamp <- as.POSIXct(df$Timestamp, format='%m/%d/%Y %H:%M:%S')
df[, Interval := as.numeric(difftime(Timestamp, .SD[1, Timestamp], units='secs') + 1)   , by=ID]
> df
              Timestamp ID   v1 Interval
 1: 2015-01-27 18:28:16  A 1.70        1
 2: 2015-01-27 18:28:17  A 1.71        2
 3: 2015-01-27 18:28:19  A 1.77        4
 4: 2015-01-27 18:28:20  A 1.79        5
 5: 2015-01-27 18:28:23  A 1.63        8
 6: 2015-01-28 22:43:08  B 7.20        1
 7: 2015-01-28 22:43:09  B 7.26        2
 8: 2015-01-28 22:43:13  B 7.16        6
 9: 2015-01-28 22:43:15  B 7.18        8
10: 2015-01-28 22:43:16  B 7.18        9
library(ggplot2)
ggplot(df, aes(x=Interval, y=v1, color=ID)) + geom_line()