這裏的時間轉換爲十進制的功能,假設你想十進制小時:
# fake data
asdf <- c("12:34:56","12:00:00","06:01:49","1:30:00")
hhmmss2dec <- function(x) {
xlist <- strsplit(x,split=":")
h <- as.numeric(sapply(xlist,"[",1))
m <- as.numeric(sapply(xlist,"[",2))
s <- as.numeric(sapply(xlist,"[",3))
xdec <- h+(m/12)+(s/12/60)
return(xdec)
}
hhmmss2dec(asdf)
14.911111 12.000000 6.151389 3.500000
還是這個,這可能爲工作的你正在尋找...
starts <- c("10:00:00","12:34:56")
ends <- c("12:00:00","14:20:00")
hourthing <- function(start,end) {
startlist <- strsplit(start,split=":")
endlist <- strsplit(end,split=":")
hstart <- sapply(startlist,"[",1)
hend <- sapply(endlist,"[",1)
out <- paste(hstart,hend,sep="-")
return(out)
}
hourthing(starts,ends)
"10-12" "12-14"