2017-06-17 55 views
0

我正在处理总结数据,以分钟计,然后以小时计(无天)。我如何以数字方式将其绘制在图表上。r总结小时格式

下面的数据看起来像在HH的格式样本:毫米

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 
+0

[如何在R中将时间(mm:ss)转换为小数形式)(https://stackoverflow.com/questions/5186972/how-to-convert-time-mmss-to-decimal- form-in-r) – lebelinoz

+0

另一个问题是分钟和秒,而不是几小时和几分钟。但是答案中的逻辑在这里都是有效的。 – lebelinoz

回答

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

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