Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中制作时隙_R - Fatal编程技术网

如何在R中制作时隙

如何在R中制作时隙,r,R,伙计们,你们能帮我给我的数据定个时间段吗 我有以下数据集 Name Arrival Time Ron 00:30 John 16:45 Sam 14:59 我想包括每次到达的时间段 Name Arrival Time Time Slot Ron 00:30 00:00-01:00 John 16:45 16:00-17:00 Sam

伙计们,你们能帮我给我的数据定个时间段吗

我有以下数据集

Name      Arrival Time  
Ron       00:30
John      16:45
Sam       14:59
我想包括每次到达的时间段

Name      Arrival Time        Time Slot
Ron       00:30               00:00-01:00
John      16:45               16:00-17:00
Sam       14:59               14:00-15:00

如何在R中执行此操作?

请查看
as.Date
strftime

a <- "00:15"
astart <- paste(
   strftime(
      as.Date(
         a,
         "%H:%M"
      ),
      "%H"
   ),
   ":00",
   sep = ""
)

a <- "00:15"
aend <- paste(
   as.integer(
      strftime(
      as.Date(
         a,
         "%H:%M"
      ),
      "%H"
      )
   ) + 1,
":00",
sep = ""
)
这里有一个策略:

arrival <- c('00:30','16:45','14:59')
a2 <- as.POSIXlt(arrival,'%H:%M',tz='')
paste(format(a2,'%H:00'),format(a2+3600,'%H:00'),sep='-')
[1] "00:00-01:00" "16:00-17:00" "14:00-15:00"

arrival另一种方法,我将时间视为完整日期时间,并将其保持为该格式:

arrivalString <- c("00:30", "16:45", "14:59")
arrival <- strptime(arrivalString, format = "%H:%M")
names <-  c("Ron", "John", "Sam")
df <- data.frame(names, arrival)

slotbegin <- as.POSIXlt(df$arrival)
slotbegin$min <-rep(0, length(slotbegin))
df <- cbind(df, slotbegin)

slotend <- as.POSIXlt(df$arrival)
slotend$min <- rep(0, length(slotend))
slotend$hour <- slotend$hour + 1
df <- cbind(df, slotend)
arrivalString <- c("00:30", "16:45", "14:59")
arrival <- strptime(arrivalString, format = "%H:%M")
names <-  c("Ron", "John", "Sam")
df <- data.frame(names, arrival)

slotbegin <- as.POSIXlt(df$arrival)
slotbegin$min <-rep(0, length(slotbegin))
df <- cbind(df, slotbegin)

slotend <- as.POSIXlt(df$arrival)
slotend$min <- rep(0, length(slotend))
slotend$hour <- slotend$hour + 1
df <- cbind(df, slotend)
  names             arrival           slotbegin             slotend
1   Ron 2013-10-09 00:30:00 2013-10-09 00:00:00 2013-10-09 01:00:00
2  John 2013-10-09 16:45:00 2013-10-09 16:00:00 2013-10-09 17:00:00
3   Sam 2013-10-09 14:59:00 2013-10-09 14:00:00 2013-10-09 15:00:00