HH:R中的MM值运算

时间:2017-12-24 15:17:14

标签: r

我将以下数据作为csv文件中时钟位置的管道中的方向提取,另存为D

 [1] 1:48  3:10  11:00 8:36  9:30  9:16  12:08 4:14  1:04  9:06  1:12  11:22 9:44  1:14  1:42  10:22 11:26 2:18  2:44 

[20] 9:22  1:00  12:58 10:14 10:02 10:02 10:24 1:04  1:28  2:22  1:56  9:42  10:06 10:16 2:28  10:18 10:38 10:00 9:52 

[39] 10:30 9:26  1:26  5:32  2:02  11:08 8:42  8:52  12:50 1:02  1:30  7:08  7:04  7:10  7:08  10:18 10:10 9:20  8:40 

[58] 9:10  9:06  8:16  8:28  6:14  10:08 10:10 1:04  1:44  5:58  6:48  5:52  9:30  10:30 10:12 10:34 10:06 2:38  4:32 

[77] 6:20  1:28  2:30  2:06  9:38  2:42  9:22  10:44 1:38  11:44 1:46  9:00  11:10 11:10 11:14

78 Levels: 1:00 1:02 1:04 1:12 1:14 1:26 1:28 1:30 1:38 1:42 1:44 1:46 1:48 1:56 10:00 10:02 10:06 10:08 ... 9:52

尝试对数据进行一些练习:

> density(D)
  

density.default(D)出错:参数'x'必须是数字

> typeof(D)

[1] "integer"

> min(D)
  

Summary.factor中的错误(c(13L,49L,30L,63L,74L,70L,37L,50L,3L,   :'min'对因素无意义

> strptime(D,format = %H-%M)
  

错误:“strptime(D,格式=%H - %”

中的意外特殊情况

我需要D作为有意义的时间格式值HH:MM进行分析和绘图。

2 个答案:

答案 0 :(得分:3)

问题中显示的D是一个因素,因此有必要先将其转换为更有意义的内容。

假设D在最后的注释中可重复显示,将数据转换为chron times类(显示为HH:MM:SS但在内部表示为一天的一小部分)所以例如1:00:00在内部表示为1/24并且绘图。 xaxt="n"省略了X默认轴,以便我们可以使用axis函数生成我们自己的自定义轴。

library(chron)
tt <- times(paste(D, "00", sep = ":"))
plot(density(tt), xaxt = "n")
axis(1, 0:24/24, 0:24) # show hours on X axis

screenshot

注意

可重现形式的输入D假定为:

D <- structure(c(13L, 49L, 30L, 63L, 74L, 70L, 37L, 50L, 3L, 68L, 
4L, 34L, 77L, 5L, 10L, 24L, 35L, 42L, 48L, 72L, 1L, 39L, 21L, 
16L, 16L, 25L, 3L, 7L, 43L, 14L, 76L, 17L, 22L, 44L, 23L, 28L, 
15L, 78L, 26L, 73L, 6L, 52L, 40L, 31L, 65L, 66L, 38L, 2L, 8L, 
59L, 58L, 60L, 59L, 23L, 19L, 71L, 64L, 69L, 68L, 61L, 62L, 55L, 
18L, 19L, 3L, 11L, 54L, 57L, 53L, 74L, 26L, 20L, 27L, 17L, 46L, 
51L, 56L, 7L, 45L, 41L, 75L, 47L, 72L, 29L, 9L, 36L, 12L, 67L, 
32L, 32L, 33L), .Label = c("1:00", "1:02", "1:04", "1:12", "1:14", 
"1:26", "1:28", "1:30", "1:38", "1:42", "1:44", "1:46", "1:48", 
"1:56", "10:00", "10:02", "10:06", "10:08", "10:10", "10:12", 
"10:14", "10:16", "10:18", "10:22", "10:24", "10:30", "10:34", 
"10:38", "10:44", "11:00", "11:08", "11:10", "11:14", "11:22", 
"11:26", "11:44", "12:08", "12:50", "12:58", "2:02", "2:06", 
"2:18", "2:22", "2:28", "2:30", "2:38", "2:42", "2:44", "3:10", 
"4:14", "4:32", "5:32", "5:52", "5:58", "6:14", "6:20", "6:48", 
"7:04", "7:08", "7:10", "8:16", "8:28", "8:36", "8:40", "8:42", 
"8:52", "9:00", "9:06", "9:10", "9:16", "9:20", "9:22", "9:26", 
"9:30", "9:38", "9:42", "9:44", "9:52"), class = "factor")

答案 1 :(得分:0)

使用G.Grothendieck构建的数据:

D <- structure(c(13L, 49L, 30L, 63L, 74L, 70L, 37L, 50L, 3L, 68L, 
                 4L, 34L, 77L, 5L, 10L, 24L, 35L, 42L, 48L, 72L, 1L, 39L, 21L, 
                 16L, 16L, 25L, 3L, 7L, 43L, 14L, 76L, 17L, 22L, 44L, 23L, 28L, 
                 15L, 78L, 26L, 73L, 6L, 52L, 40L, 31L, 65L, 66L, 38L, 2L, 8L, 
                 59L, 58L, 60L, 59L, 23L, 19L, 71L, 64L, 69L, 68L, 61L, 62L, 55L, 
                 18L, 19L, 3L, 11L, 54L, 57L, 53L, 74L, 26L, 20L, 27L, 17L, 46L, 
                 51L, 56L, 7L, 45L, 41L, 75L, 47L, 72L, 29L, 9L, 36L, 12L, 67L, 
                 32L, 32L, 33L), .Label = c("1:00", "1:02", "1:04", "1:12", "1:14", 
                                            "1:26", "1:28", "1:30", "1:38", "1:42", "1:44", "1:46", "1:48", 
                                            "1:56", "10:00", "10:02", "10:06", "10:08", "10:10", "10:12", 
                                            "10:14", "10:16", "10:18", "10:22", "10:24", "10:30", "10:34", 
                                            "10:38", "10:44", "11:00", "11:08", "11:10", "11:14", "11:22", 
                                            "11:26", "11:44", "12:08", "12:50", "12:58", "2:02", "2:06", 
                                            "2:18", "2:22", "2:28", "2:30", "2:38", "2:42", "2:44", "3:10", 
                                            "4:14", "4:32", "5:32", "5:52", "5:58", "6:14", "6:20", "6:48", 
                                            "7:04", "7:08", "7:10", "8:16", "8:28", "8:36", "8:40", "8:42", 
                                            "8:52", "9:00", "9:06", "9:10", "9:16", "9:20", "9:22", "9:26", 
                                            "9:30", "9:38", "9:42", "9:44", "9:52"), class = "factor")

将因子转换为字符向量:

D_chr <- as.character(D)

然后:

D_time <-  format(D_chr, format = "%H:%M:%S")
D_hms <- as.POSIXct(D_time, format = "%H:%M")
ClockTime <- data.frame(D_time,D_hms)

使用ggplot2包,您可以这样制作密度图:

library(ggplot2)

ggplot(ClockTime) + theme_bw() +
    geom_density(aes(D_hms,y=..scaled..)) +
    scale_x_datetime(date_breaks="1 hours",date_labels ="%I %M") +
    xlab("Hour")

enter image description here