如何将时间间隔改为R?

时间:2015-11-02 08:06:04

标签: r time

Date,Time,Lots,Status
"10-28-15","00:04:50","13-09","1111111110000000"
"10-28-15","00:04:50","13-10","1111100000000000"
"10-28-15","00:04:50","13-11","1111111011100000"
"10-28-15","00:04:50","13-12","1111011111000000"
"10-28-15","00:04:57","13-13","1111111111000000"
"10-28-15","00:04:57","13-14","1111111111110000"
"10-28-15","00:04:57","13-15","1111111100000000"
"10-28-15","00:04:57","13-16","1111111111000000"
"10-28-15","00:05:04","13-17","1111111110000000"
"10-28-15","00:05:04","13-18","1111101100000000"
"10-28-15","00:05:04","13-19","1111111111100000"
"10-28-15","00:05:04","13-20","1111111111100000"
"10-28-15","00:05:11","13-21","1111110100000000"
"10-28-15","00:05:11","13-22","1000011111100000"
"10-28-15","00:05:11","13-23","1101011111110000"
"10-28-15","00:05:11","13-24","1111111111000000"
"10-28-15","00:05:19","13-25","1011000000000000"
"10-28-15","00:05:19","13-26","0000000000000000"
"10-28-15","00:05:19","13-27","1111011110000000"
"10-28-15","00:05:19","13-28","1010000000000000"
说pfr高于“样本”,我需要将时间转换为15分钟的时间间隔。我怎么做?例如:我每小时有4个间隔。 00:04:50将进入00:04:45-00:04:50。谢谢!

我尝试过使用:

format(as.POSIXlt(as.POSIXct('2000-1-1', "UTC") + round(as.numeric(sample$V3)/300)*300), format = "%H:%M:%S")

2 个答案:

答案 0 :(得分:1)

我希望我能正确理解你的问题,但我认为你可以这样做:

# example data frame:
myDat <- data.frame(date = rep("2010-08-15",30),
                time = sprintf("%02i:%02i:%02i",rep(12:14,each=10),rep(c(0,15,16,29:31,44:45,59,1),3),sample(1:59)[30]))

#produce a datetime variable
myDat$datetime <- strptime(x = paste(myDat$date,myDat$time),format = "%Y-%m-%d %H:%M:%S")
# extract the minutes
myDat$min <- as.integer(as.character(myDat$datetime,format="%M"))
# find the interval to put them in
myDat$ival <- findInterval(myDat$min,c(15,30,45,60)); myDat$ival <- factor(myDat$ival)
levels(myDat$ival) <- c("00","15","30","45")
# concatenate minute interval and hour
myDat$timeIval <- sprintf("%s:%s:00",as.character(myDat$datetime,format="%H"),myDat$ival)
myDat[order(myDat$datetime),]

答案 1 :(得分:0)

我首先将数据转换为POSIXct

time <- as.POSIXct(
  strptime(paste0(df$column1, " ", df$column2), format="%m-%d-%y %H:%M:%S")
)

然后使用seq.POSIXctcut生成15分钟间隔的因子变量。

interval <- cut(
  time,
  breaks = seq(starttime, endtime, as.difftime(15, units="mins))
)

您可能希望在休息时添加-InfInf,以避免生成NA值。