2017-06-17 8 views
0

Ich arbeite mit verdichteten Daten, die die Zeit in Minuten und Stunden (keine Tage) zählt. Wie mache ich es numerisch so, dass es in einem Diagramm geplottet werden kann.r zusammengefasst Stundenformat

Unten finden Sie ein Beispiel, wie die Daten aussehen im Format 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 
+0

Mögliches Duplikat von [Wie konvertiert man Zeit (mm: ss) in Dezimalform in R] (https://stackoverflow.com/questions/5186972/how-to-convert-time-mmss-to-decimal- form-in-r) – lebelinoz

+0

Die andere Frage ist Minuten und Sekunden statt Stunden und Minuten. Aber die Logik in den Antworten sind alle hier gültig. – lebelinoz

Antwort

1

Wenn Sie zu lange Form umformen geben, die beide Konvertieren und Plotten sind einfacher:

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

Dadurch werden die Zeiten in Minuten

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 
Verwandte Themen