我的数据框(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毫秒填充我的dataframe列的剩余部分(这是很大的),以便下一列看起来像这样,依此类推...
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)
但是它返回一个错误(见下文)
Error in seq.POSIXt(datasetname2[6, 1], units = "seconds", by = 0.1) :
'from' must be a "POSIXt" object
答案 0 :(得分:0)
这可能对您有用:
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")