r的小数小时(今天除外)

时间:2015-06-17 12:40:12

标签: r

对于示例数据框:

light <- structure(list(daylight.hours = structure(c(62L, 22L, 60L, 58L, 
                                            34L, 37L), .Label = c("07:12:05", "07:14:41", "07:18:24", "07:28:59", 
                                                                  "07:31:07", "07:45:51", "07:48:08", "07:51:29", "07:52:06", "07:58:18", 
                                                                  "08:01:16", "08:07:25", "08:10:08", "08:18:16", "08:23:33", "08:27:03", 
                                                                  "08:30:36", "08:34:13", "08:41:35", "08:46:01", "08:53:52", "08:54:17", 
                                                                  "09:31:16", "09:35:29", "09:39:44", "10:27:19", "10:31:45", "10:36:12", 
                                                                  "11:53:41", "12:11:39", "12:16:10", "12:20:23", "12:34:10", "14:18:26", 
                                                                  "14:22:41", "14:26:55", "14:35:21", "14:39:49", "14:44:00", "14:48:09", 
                                                                  "14:54:29", "14:59:08", "15:03:18", "15:11:01", "15:15:38", "15:15:52", 
                                                                  "15:19:09", "15:58:22", "16:07:10", "16:08:33", "16:24:12", "16:27:14", 
                                                                  "16:42:57", "16:55:32", "16:57:52", "17:00:06", "17:02:15", "17:03:49", 
                                                                  "17:04:17", "17:05:24", "17:06:14", "17:06:53", "17:08:05", "17:09:38", 
                                                                  "17:11:04", "17:12:24", "17:13:26", "17:13:47", "17:14:22", "17:14:32", 
                                                                  "17:14:42", "17:14:44", "17:15:39", "17:15:40", "17:16:22", "17:16:51", 
                                                                  "17:17:55"), class = "factor"), school.id = c(4L, 4L, 4L, 4L, 
                                                                                                                14L, 14L)), .Names = c("daylight.hours", "school.id"), row.names = c(NA, 
                                                                                                                                                                                     6L), class = "data.frame")

我想创建另一个名为d.daylight的变量,将日光时数变量更改为小数。 (即18:30:00将阅读18.5)

当我使用以下内容时,它会自动显示今天的日期,而不是我想要的日期(一切都在24小时之内)。

light$d.daylight <- as.POSIXlt(light$daylight.hours, format="%H:%M:%S")

有人可以告诉我如何纠正这个问题吗?

1 个答案:

答案 0 :(得分:0)

如果您需要处理时间(没有日期),则包chron中的times函数非常有用。

library(chron)
light$d.daylight <- as.numeric(times(light$daylight.hours)) * 24
#  daylight.hours school.id d.daylight
#1       17:06:53         4  17.114722
#2       08:54:17         4   8.904722
#3       17:05:24         4  17.090000
#4       17:03:49         4  17.063611
#5       14:18:26        14  14.307222
#6       14:35:21        14  14.589167