在R中排序时间(小时)

时间:2015-03-29 18:53:27

标签: r time

我有来自excel的以下数据集,数字表示小时数,如何将其转换为灵活的数据集,我可以按照我想要的方式安排(订购)我的数据集。

我使用lubridate尝试了以下内容,但这并不能让我安排desc或asc。

Category   Time1    Time2
1        A 0:30:00 24:00:00
2        B 1:00:00 23:23:00
3        C 2:30:00 23:00:59
4        D 3:00:00 45:00:00

> dput(t1)
structure(list(Category = c("A", "B", "C", "D"), Time1 = c("0:30:00", 
"1:00:00", "2:30:00", "3:00:00"), Time2 = c("24:00:00", "23:23:00", 
"23:00:59", "45:00:00"), hr1 = structure(c(0, 0, 0, 0), year = c(0, 
0, 0, 0), month = c(0, 0, 0, 0), day = c(0, 0, 0, 0), hour = c(0, 
1, 2, 3), minute = c(30, 0, 30, 0), class = structure("Period", package = "lubridate")), 
    hr2 = structure(c(0, 0, 59, 0), year = c(0, 0, 0, 0), month = c(0, 
    0, 0, 0), day = c(0, 0, 0, 0), hour = c(24, 23, 23, 45), minute = c(0, 
    23, 0, 0), class = structure("Period", package = "lubridate"))), .Names = c("Category", 
"Time1", "Time2", "hr1", "hr2"), row.names = c(NA, -4L), class = "data.frame")

RCODE

t1<-read.csv("time1.csv", header=TRUE, sep=",",stringsAsFactors=FALSE)
library(lubridate)
t1$hr1<-hms(t1$Time1)
t1$hr2<-hms(t1$Time2)

输出

Category   Time1    Time2       hr1        hr2
1        A 0:30:00 24:00:00    30M 0S  24H 0M 0S
2        B 1:00:00 23:23:00  1H 0M 0S 23H 23M 0S
3        C 2:30:00 23:00:59 2H 30M 0S 23H 0M 59S
4        D 3:00:00 45:00:00  3H 0M 0S  45H 0M 0S

1 个答案:

答案 0 :(得分:0)

您正在寻找的是?difftime功能。 但是,由于您将小时数编码为大于24小时,因此无效。

一个肮脏的解决方法是在几秒钟内对所有内容进行编码并从那里开始:

将小时[1],分钟[2],秒[3]字符的向量转换为秒的函数:

convS<-function(x) {
    x<-as.numeric(x)
    seconds<-c(3600,60,1)
    x<-x*seconds
    sum(x)
  }

将该函数应用于具有2个时间变量的分割字符向量,并将输出放在两个新变量中,并以秒为单位经过时间:

t1$Time1Secs<-unlist(lapply(strsplit(t1$Time1,":"),convS))
t1$Time2Secs<-unlist(lapply(strsplit(t1$Time2,":"),convS))

所以现在你有数字变量,时间差以秒为单位,无论如何都可以为数据集排序。