Liam
Liam

Reputation: 53

Plotting a histogram using datetime data from strptime

Here is what my data look like

Tue Feb 11 18:21:45 +0000 2014
Tue Feb 11 18:22:03 +0000 2014
Tue Feb 11 18:22:05 +0000 2014

Each row is an occurrence of an event. I'm trying to plot this data into a histogram.

Since the times are down to hours, minutes and seconds and I have a weeks worth of data. My histogram bins need to be based on days. So i can see actual significant change.

Here's my code so far..

Time_Data <- read.csv("Twitter_Data_2.csv")

Time_Data_Strptime <- strptime(data[,1], format ="%a%b%d %I:%M:%S %z%y")

followed by

hist(x=Time_Data_Strptime[,1], breaks="hours")

Im getting this error:

Error in lapply(X = x, FUN = "[", ..., drop = drop) : argument is missing, with no default

I'm new to R studio, so any help or advice would be greatly appreciated.

edit: Sample from typing dput(head(Time_Data))

"Wed Feb 12 07:12:15 +0000 2014", "Wed Feb 12 07:12:55 +0000 2014", 
"Wed Feb 12 07:13:48 +0000 2014", "Wed Feb 12 07:15:53 +0000 2014", 
"Wed Feb 12 07:16:28 +0000 2014", "Wed Feb 12 07:20:11 +0000 2014", 
"Wed Feb 12 07:20:39 +0000 2014", "Wed Feb 12 07:20:47 +0000 2014",

edit2: Sample from typing Time_Data_strptime

   [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
  [42] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
  [83] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [124] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

edit3: Sample from Time_Data_strptime

[1] "2014-02-11 23:19:56" "2014-02-11 23:20:04"
[3] "2014-02-11 23:21:06" "2014-02-11 23:21:15"
[5] "2014-02-11 23:21:30" "2014-02-11 23:22:02"

Upvotes: 1

Views: 779

Answers (1)

loki
loki

Reputation: 10350

This is what works for me:

Time_Data <- read.table(text = "Tue Feb 11 18:21:45 +0000 2014
Tue Feb 11 18:22:03 +0000 2014
Tue Feb 11 13:22:05 +0000 2014", head = F, sep = ";", stringsAsFactors = F)
  1. Using POSIXct instead of strptime since there is a hist.POSIXct method. (First change your system locale to the english one if necessary)

    Sys.setlocale("LC_ALL","English")
    Time_Data_Strptime <- as.POSIXct(Time_Data[,1], format ="%a %b %d %H:%M:%S %z %Y")
    
  2. Then use hist like this:

    hist(x=Time_Data_Strptime, breaks = "hours", freq = TRUE)
    

Further information about that can be found in ??hist.POSIXt

enter image description here

Upvotes: 1

Related Questions