2013-10-09 86 views
1

你們會幫我製作一段我的數據嗎?如何在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  14:59    14:00-15:00 

怎麼辦呢R中?

回答

2

這裏有一個策略:

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" 
0

看一看as.Datestrftime

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 = "" 
) 

輸出:

> astart 
[1] "00:00" 
> aend 
[1] "1:00" 
1

另一種方法,我把時間作爲全datetime和保持他們的格式如下:

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