如何使用 seq() 以毫秒(十秒)为增量创建 date/times 的列

How to use seq() to create column of date/times with increments of milliseconds (deciseconds)

我的数据框 (df) 如下所示:

ID  CH_1   CH_2  CH_3  CH_4             date_time
1 -10096 -11940 -9340 -9972 2018-07-24 10:45:01.1
2 -10088 -11964 -9348 -9960                  <NA>
3 -10084 -11940 -9332 -9956                  <NA>
4 -10088 -11956 -9340 -9960                  <NA>

最后一列,date_time编码为POSIXct格式。我需要做的是用增加的分秒或 100 毫秒来填充我的数据框列的其余部分(它非常大),以便下一列看起来像这样,依此类推...

ID  CH_1   CH_2  CH_3  CH_4             date_time
1 -10096 -11940 -9340 -9972 2018-07-24 10:45:01.1
2 -10088 -11964 -9348 -9960 2018-07-24 10:45:01.2
3 -10084 -11940 -9332 -9956 2018-07-24 10:45:01.3
4 -10088 -11956 -9340 -9960 2018-07-24 10:45:01.4

我试过使用以下方法,

startDate <- df[["date_time"]][1]
datasetname2$date_time = as.POSIXct(startDate) + seq.POSIXt(datasetname2[6,1], 
units="seconds", by=.1)

但它 returns 是一个错误(见下文)

Error in seq.POSIXt(datasetname2[6, 1], units = "seconds", by = 0.1) :
'from' must be a "POSIXt" object

这可能对你有用:

library(lubridate)
df <- data.frame(ID = 1:4, 
                 CH_1 = rep(c(-10096, -10088, -10084, -10088), 4),
                 CH_2 = rep(c(-11940, -11964, -11940, -11956), 4),
                 date_time = c("2018-07-24 10:45:01.1", rep(NA, 15)))

df$date_time <- as.POSIXct(df$date_time, format= "%F %H:%M:%OS2", tz = "UTC")
df$date_time <- seq(from = df$date_time[1], to = df$date_time[1] + (nrow(df)-1)*0.1, by = 0.10)
format(df$date_time, "%OS3")