tick_count列的单位是100毫秒。我想在date_time加上tick_count时间。但结果不能正确执行添加。 例如,第二个值应该是“2014-08-15 12:51:02.6 EEST”而不是“2014-08-15 12:51:02.5 EEST”,我不知道为什么会这样。它对我的结果造成了严重的影响,因为有时它导致两行或三行具有相同的时间。 如何应对这种情况?
>head(status306$date_time)
#[1] "2014-08-15 12:51:01 EEST" "2014-08-15 12:51:01 EEST" "2014-08-15 12:51:01 EEST"
#[4] "2014-08-15 12:51:01 EEST" "2014-08-15 12:51:01 EEST" "2014-08-15 12:51:01 EEST"
> head(status306$tick_count/10)
#[1] 0.0 1.6 1.7 1.1 2.6 2.8
> head(status306$date_time +status306$tick_count/10)
#[1] "2014-08-15 12:51:01.0 EEST" "2014-08-15 12:51:02.5 EEST" "2014-08-15 12:51:02.7 EEST"
#[4] "2014-08-15 12:51:02.0 EEST" "2014-08-15 12:51:03.5 EEST" "2014-08-15 12:51:03.7 EEST"
更新
这是原始数据框架,我才知道:p
structure(list(grint = c("C51CCF4CAACGGCC44", "C55CCF4CCCCGGCC44",
"C55CCF4CCCCGGCC44", "C55CCF4CCCCGGCC44", "C55CCF4CCC011CC44",
"C55CCF4CCC011CC44"), dint = c("0000010100001010000000000001110000000001100001000000",
"0000010100001010000000000001110000000001100001000000", "0100010100001010000000000001110000000001100001000000",
"0100010100001011000000000001110000000001100001000000", "0000010100001011000000000001110000000001100001000000",
"0000010100001011000000000000100000000001100001000000"), tick_count = c(0L,
16L, 17L, 11L, 26L, 28L), date_time = structure(c(1408096261,
1408096261, 1408096261, 1408096261, 1408096261, 1408096261), class = c("POSIXct",
"POSIXt"), tzone = ""), detector_signal_levels = c("135,1,163,1,165,5,167,167,5,167,5,163,3,169,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,",
"135,5,163,1,165,5,167,167,5,167,5,163,3,169,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,",
"135,5,163,1,165,5,167,167,5,167,5,163,3,169,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,",
"135,5,163,1,165,5,167,7,5,167,5,163,3,169,77,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,",
"135,5,163,1,165,5,167,7,9,167,1,163,3,169,121,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,",
"135,5,163,1,165,5,167,7,9,15,1,163,3,169,123,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
)), .Names = c("grint", "dint", "tick_count", "date_time", "detector_signal_levels"
), row.names = c(NA, 6L), class = "data.frame")
答案 0 :(得分:1)
尝试:(如果您的输入是character
类)
date_time <- c("2014-08-15 12:51:01", "2014-08-15 12:51:01", "2014-08-15 12:51:01",
"2014-08-15 12:51:01", "2014-08-15 12:51:01", "2014-08-15 12:51:01")
date_time1 <- strptime(date_time, format="%Y-%m-%d %H:%M:%S", tz="EEST")
v1 <- c(0.0, 1.6,1.7,1.1, 2.6, 2.8)
date_time1$sec <- date_time1$sec+v1
op <- options(digits.secs=3)
date_time1
#[1] "2014-08-15 12:51:01.0 EEST" "2014-08-15 12:51:02.6 EEST"
#[3] "2014-08-15 12:51:02.7 EEST" "2014-08-15 12:51:02.1 EEST"
#[5] "2014-08-15 12:51:03.6 EEST" "2014-08-15 12:51:03.8 EEST"
我想我可以重现你的结果:
date_time1 <- as.POSIXct(date_time, format="%Y-%m-%d %H:%M:%S", tz="EEST")
date_time1+v1
#[1] "2014-08-15 12:51:01.0 EEST" "2014-08-15 12:51:02.5 EEST"
#[3] "2014-08-15 12:51:02.7 EEST" "2014-08-15 12:51:02.0 EEST"
#[5] "2014-08-15 12:51:03.5 EEST" "2014-08-15 12:51:03.7 EEST"
转换为POSIXlt
,您可以获得预期的结果(但是,我不确定您的预期结果是否正确)
status306$date_time<- as.POSIXlt(status306$date_time)
status306$date_time$sec <- with(status306, date_time$sec + tick_count/10)
status306$date_time
#[1] "2014-08-15 05:51:01.0 EDT" "2014-08-15 05:51:02.6 EDT"
#[3] "2014-08-15 05:51:02.7 EDT" "2014-08-15 05:51:02.1 EDT"
#[5] "2014-08-15 05:51:03.6 EDT" "2014-08-15 05:51:03.8 EDT"
status306$date_time[4] <- as.POSIXct("2014-08-15 12:53:59")
status306$date_time<- as.POSIXlt(status306$date_time)
val <- with(status306, date_time$sec + tick_count/10)
status306$date_time$sec <- ifelse(val >=60, val-60, val)
status306$date_time$min <- ifelse(val >=60, status306$date_time$min+1, status306$date_time$min)
status306$date_time
#[1] "2014-08-15 05:51:01.0 EDT" "2014-08-15 05:51:02.6 EDT"
#[3] "2014-08-15 05:51:02.7 EDT" "2014-08-15 12:54:00.1 EDT"
#[5] "2014-08-15 05:51:03.6 EDT" "2014-08-15 05:51:03.8 EDT"
options(op)