0

I pull following data as orientation in pipe in clock position from csv file save as 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

Try to do some exercise to data :

> density(D)

Error in density.default(D) : argument 'x' must be numeric

> typeof(D)

[1] "integer"

> min(D)

Error in Summary.factor(c(13L, 49L, 30L, 63L, 74L, 70L, 37L, 50L, 3L, : ‘min’ not meaningful for factors

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

Error: unexpected SPECIAL in "strptime(D,format = %H-%"

I need D as meaningful value in time format HH:MM to do analysis and ploting.

PKumar
  • 10,971
  • 6
  • 37
  • 52
  • Your times' format is `%H:%M`, not `%H-%M`: should be `strptime(D, format="%H:%M")`. – Abdou Dec 24 '17 at 15:21
  • I see that data is between 1-12 hrs: Are you describing the angle between the hands of analogue clock? https://en.wikipedia.org/wiki/Clock_angle_problem. Or is it a measure of angle 0-360 deg (why isn't it then in decimals?). – dmi3kno Dec 24 '17 at 15:31
  • Can you make your example so that it's copy/pastable into an R session? See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info. – Roman Luštrik Dec 24 '17 at 15:54
  • @dmci3kno, There are actually 3 values > 12:00:00, namely 12:08:00 12:50:00 12:58:00 – G. Grothendieck Dec 24 '17 at 16:30

2 Answers2

3

The D shown in the question is a factor so it will be necessary to convert it to something more meaningful for your purposes first.

Assuming D is as shown reproducibly in the Note at the end, convert the data to chron times class (which displays as HH:MM:SS but is internally represented as a fraction of a day so that for example 1:00:00 is represented as 1/24 internally) and plot. xaxt="n" omits the X default axis so that we can produce our own custom axis using the axis function.

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

Note

The input D in reproducible form is assumed to be:

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")
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

Using data structured by 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")

Convert factor into a character vector:

D_chr <- as.character(D)

Then:

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)

Using ggplot2 package you can make a density plot in this way:

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

Scipione Sarlo
  • 1,470
  • 1
  • 17
  • 31