0

For a sample dataframe:

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

I want to create another variable called d.daylight to change the daylight hours variable to a decimal. (i.e. 18:30:00 would read 18.5)

When I use the following it automatically puts todays date which is not what I am after (everything is under 24 hours).

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

Could anyone advise me how to rectify this?

KT_1
  • 8,194
  • 15
  • 56
  • 68
  • 2
    Your `dput` would be much smaller if you'd `light$daylight.hours <- factor(light$daylight.hours)` before posting. You have many empty levels here. – David Arenburg Jun 17 '15 at 12:44
  • 1
    You may check [here](http://stackoverflow.com/questions/5186972/how-to-convert-time-mmss-to-decimal-form-in-r) – akrun Jun 17 '15 at 12:44
  • 1
    In order to reach the input in @akruns link you could do something like `gsub("(\\d{2}):(\\d{2}).*", "\\1:\\2", light$daylight.hours)`. Or just for fun, you could also just do `library(gsubfn) ; as.numeric(gsubfn("(\\d{2}):(\\d{2}).*", ~ as.numeric(x) + round(as.numeric(y)/60, 2), as.character(light$daylight.hours)))` – David Arenburg Jun 17 '15 at 12:54
  • possible duplicate of [R How to convert time to decimal](http://stackoverflow.com/questions/21781311/r-how-to-convert-time-to-decimal) – germcd Jun 17 '15 at 13:43

1 Answers1

0

The times function from package chron is useful if you need to deal with times (without dates).

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
Roland
  • 127,288
  • 10
  • 191
  • 288