r汇总小时格式

时间:2017-06-17 22:57:19

标签: r time

我正在使用汇总数据计算以分钟为单位的时间,然后是小时(无日期)。如何将其设置为可以在图表上绘制的数字。

以下是hh:mm

格式的数据样本示例
set.seed(121)

df <- data.frame(hspt =  letters[1:3],
                 Jan = paste0(sample(1:1000, 3, replace=TRUE),":", 
                     stringr::str_pad(sample(0:60, 3, replace=TRUE), 2, pad = "0")),
                 Feb = paste0(sample(1:1000, 3, replace=TRUE),":", 
                     stringr::str_pad(sample(0:60, 3, replace=TRUE), 2, pad = "0")), 
                 Mar = paste0(sample(1:1000, 3, replace=TRUE),":", 
                     stringr::str_pad(sample(0:60, 3, replace=TRUE), 2, pad = "0")), 
                 stringsAsFactors = F)

df
  hspt    Jan    Feb    Mar
 1    a 763:28 255:37 289:49
 2    b 551:37 947:07 136:46
 3    c 422:14 783:29 618:56

2 个答案:

答案 0 :(得分:1)

如果您重塑为长形,转换和绘图都会更容易:

library(tidyverse)
set.seed(121)

df <- data.frame(hspt =  letters[1:3],
                 Jan = paste0(sample(1:1000, 3, replace=TRUE),":", stringr::str_pad(sample(0:60, 3, replace=TRUE), 2, pad = "0")),
                 Feb = paste0(sample(1:1000, 3, replace=TRUE),":", stringr::str_pad(sample(0:60, 3, replace=TRUE), 2, pad = "0")), 
                 Mar = paste0(sample(1:1000, 3, replace=TRUE),":", stringr::str_pad(sample(0:60, 3, replace=TRUE), 2, pad = "0")), 
                 stringsAsFactors = F)

df2 <- df %>% 
    gather(month, time, -hspt) %>%    # reshape to long
    separate(time, c('hours', 'minutes'), convert = TRUE) %>% 
    mutate(month = factor(month, month.abb[1:3], ordered = TRUE),    # for x-axis order
           time = 60 * hours + minutes) 

df2
#>   hspt month hours minutes  time
#> 1    a   Jan   400      46 24046
#> 2    b   Jan   952      33 57153
#> 3    c   Jan   544      25 32665
#> 4    a   Feb   468      15 28095
#> 5    b   Feb   614      57 36897
#> 6    c   Feb   238      47 14327
#> 7    a   Mar   617      17 37037
#> 8    b   Mar   124       8  7448
#> 9    c   Mar   478      37 28717

ggplot(df2, aes(month, time, color = hspt, group = hspt)) + geom_line()

答案 1 :(得分:0)

这将以分钟为单位给出时间

library(lubridate)

df
#   hspt    Jan    Feb    Mar
# 1    a 400:46 468:15 617:17
# 2    b 952:33 614:57 124:08
# 3    c 544:25 238:47 478:37

df[, -1] <- sapply(df[, -1], function(x) hour(hm(x))*60 + minute(hm(x)))
df
#   hspt   Jan   Feb   Mar
# 1    a 24046 28095 37037
# 2    b 57153 36897  7448
# 3    c 32665 14327 28717
相关问题