将时间转换为十进制小时或十进制分钟

时间:2019-07-02 18:15:43

标签: r datetime lubridate

我有行为观察,观察的开始时间和每种行为的时间。自观察开始以来,开始时间的格式为hh:mm,行为时间为mm:ss。

我想将自午夜以来数小时的开始时间转换为数值(例如12:30将为12.5),将行为时间转换为自观察开始以来的分钟(例如1:30将是1.5)。

到目前为止,我已经尝试了以下方法:

#1 - results in NA values
library(lubridate)
df <- mutate(df,
             startTimeH = strtoi(as.difftime(as.character(startTime), format = "%H:%M:%S", units = "hours")),
             behavTimeM = strtoi(as.difftime(as.character(behavTime), format = "%M:%S", units = "mins")))

#2 - unclear what happens with this approach, matrix-like
df <- mutate(df,
             startTimeH = hms(startTime),
             behavTimeM = ms(behavTime))

这是我的数据:

> dput(df$startTime[1:50])
structure(c(36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
36L, 36L, 36L, 36L, 36L, 36L, 36L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 33L, 33L, 33L), .Label = c("10:04", 
"10:07", "10:08", "10:23", "10:46", "10:48", "11:06", "12:15", 
"12:18", "12:46", "12:53", "13:11", "13:12", "13:32", "13:39", 
"14:08", "14:11", "14:43", "15:56", "16:28", "16:41", "17:07", 
"17:20", "17:30", "17:33", "17:36", "18:07", "18:24", "6:55", 
"7:00", "8:09", "8:11", "8:42", "8:50", "9:12", "9:20", "9:22", 
"9:31", "9:33", "9:49"), class = "factor")
> dput(df$behavTime[1:50])
structure(c(18L, 129L, 130L, 136L, 140L, 180L, 181L, 183L, 202L, 
227L, 228L, 261L, 263L, 264L, 286L, 22L, 23L, 149L, 154L, 155L, 
157L, 160L, 177L, 178L, 184L, 186L, 199L, 200L, 220L, 229L, 230L, 
44L, 45L, 66L, 67L, 80L, 88L, 90L, 91L, 110L, 116L, 120L, 246L, 
249L, 257L, 268L, 269L, 9L, 11L, 101L), .Label = c("", "0:22", 
"0:48", "0:49", "0:52", "0:57", "1:02", "1:03", "1:05", "1:06", 
"1:07", "1:10", "1:14", "1:16", "1:25", "1:34", "1:35", "1:48", 
"1:50", "1:53", "10:08", "10:13", "10:14", "10:18", "10:28", 
"10:29", "10:31", "10:32", "10:33", "10:34", "10:36", "10:37", 
"10:38", "10:39", "10:40", "10:41", "10:43", "10:54", "11:02", 
"11:07", "11:17", "11:20", "11:27", "11:28", "11:29", "11:41", 
"11:42", "11:51", "11:53", "11:59", "12:04", "12:07", "12:08", 
"12:12", "12:14", "12:16", "12:18", "12:20", "12:21", "12:22", 
"12:29", "12:33", "12:34", "12:39", "12:40", "12:43", "12:44", 
"12:46", "12:47", "12:48", "12:50", "12:51", "12:52", "12:53", 
"12:55", "12:56", "12:57", "12:58", "13:04", "13:06", "13:10", 
"13:12", "13:14", "13:15", "13:17", "13:18", "13:20", "13:21", 
"13:23", "13:28", "13:35", "13:36", "13:37", "13:38", "13:39", 
"13:41", "13:42", "13:49", "13:50", "13:51", "13:55", "13:56", 
"13:57", "13:58", "14:00", "14:07", "14:08", "14:12", "14:16", 
"14:20", "14:21", "14:22", "14:23", "14:29", "14:31", "14:35", 
"14:36", "14:38", "14:40", "14:41", "14:47", "14:49", "14:50", 
"14:51", "14:54", "14:59", "15:04", "2:01", "2:02", "2:04", "2:09", 
"2:10", "2:15", "2:16", "2:17", "2:18", "2:19", "2:20", "2:24", 
"2:25", "2:26", "2:28", "2:31", "2:32", "2:36", "2:41", "2:43", 
"2:44", "2:45", "2:54", "2:58", "3:03", "3:05", "3:06", "3:07", 
"3:12", "3:15", "3:16", "3:18", "3:20", "3:27", "3:33", "3:41", 
"3:42", "3:43", "3:44", "3:47", "3:48", "3:49", "3:54", "3:56", 
"4:10", "4:12", "4:13", "4:14", "4:15", "4:16", "4:18", "4:20", 
"4:21", "4:22", "4:23", "4:24", "4:25", "4:26", "4:30", "4:32", 
"4:34", "4:37", "4:38", "4:39", "4:40", "4:43", "4:44", "4:45", 
"4:47", "4:48", "4:49", "4:50", "4:51", "4:53", "4:54", "4:59", 
"5:00", "5:07", "5:13", "5:14", "5:20", "5:23", "5:27", "5:28", 
"5:32", "5:33", "5:34", "5:36", "5:37", "5:38", "5:39", "5:42", 
"5:45", "5:46", "5:48", "5:50", "5:51", "5:52", "5:53", "5:55", 
"5:56", "5:57", "5:58", "6:00", "6:01", "6:04", "6:06", "6:07", 
"6:13", "6:16", "6:17", "6:18", "6:19", "6:20", "6:21", "6:28", 
"6:33", "6:34", "6:40", "6:41", "6:42", "6:45", "6:46", "6:48", 
"6:51", "6:53", "6:54", "6:55", "6:56", "6:58", "6:59", "7:04", 
"7:07", "7:10", "7:11", "7:13", "7:15", "7:16", "7:17", "7:18", 
"7:20", "7:21", "7:22", "7:23", "7:26", "7:27", "7:30", "7:32", 
"7:33", "7:35", "7:37", "7:38", "7:39", "7:42", "7:43", "7:44", 
"7:50", "7:51", "7:57", "7:58", "8:00", "8:02", "8:03", "8:08", 
"8:10", "8:13", "8:17", "8:18", "8:19", "8:20", "8:22", "8:23", 
"8:27", "8:33", "8:35", "8:36", "8:38", "8:41", "8:42", "8:47", 
"8:49", "8:54", "8:55", "9:02", "9:08", "9:11", "9:12", "9:13", 
"9:14", "9:16", "9:24", "9:30", "9:33", "9:34", "9:36", "9:39"
), class = "factor")

1 个答案:

答案 0 :(得分:1)

您可以使用一些dplyr和tidyr代码来做到这一点:

library(dplyr)
library(tidyr)
# I read your data to two different objects
df <- tibble(startTime, behavTime)

df_new <- df %>% 
  # your data is a factor (I assume from wrong options during reading it in)
  mutate_if(is.factor, as.character) %>%
  # We seperate the column into hours, minutes and seconds
  separate(startTime, into = c("startTime_hh", "startTime_mm"), ":") %>% 
  separate(behavTime, into = c("behavTime_mm", "behavTime_ss"), ":") %>% 
  # We convert the time into fractions of an hour
  mutate(startTime_hh = as.numeric(startTime_hh),
         startTime_mm = as.numeric(startTime_mm) / 60,
         behavTime_mm = as.numeric(behavTime_mm) / 60,
         behavTime_ss = as.numeric(behavTime_ss) / 3600) %>% 
  # lastly we reassemble the times
  mutate(startTime = startTime_hh + startTime_mm,
         behavTime = behavTime_mm + behavTime_ss)