【问题标题】:r summarized hours formatr 汇总时间格式
【发布时间】:2017-11-20 11:11:20
【问题描述】:

我正在处理以分钟和小时(没有天)计算时间的汇总数据。如何以可以绘制在图表上的方式使其成为数字。

以下是 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

【问题讨论】:

标签: r time


【解决方案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()

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2012-06-04
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 2015-06-03
      • 1970-01-01
      相关资源
      最近更新 更多