1

I have POSIXct dates stored in a vector, and I want to plot/extract the number of occurrences for each date.

head(dates1, 10)
 [1] NA               NA               NA               "2012-01-19 UTC"
 [5] NA               NA               NA               "2013-04-02 UTC"
 [9] "2012-10-04 UTC" "2012-12-20 UTC"

The closest I get is this:

hist(dates1, breaks = 27, freq = TRUE, ylim = c(0, 80))

histogram

However, this is quite crude. What I would like to do is to convert the data to a vector with counts of each date so that I can do more computations and plots on it. How can I accomplish this?

histelheim
  • 4,938
  • 6
  • 33
  • 63
  • 2
    A developmental comment is more helpful than a downvote... – histelheim Dec 26 '14 at 21:11
  • possible duplicate of [Counting the Number of Elements With The Values of x in a Vector?](http://stackoverflow.com/questions/1923273/counting-the-number-of-elements-with-the-values-of-x-in-a-vector) – Henrik Dec 26 '14 at 21:11
  • Those look like class `Date` values to me, not `POSIXct` date-times. Perhaps something like `x <- sample(Sys.Date()+0:9, 50, TRUE); plot(table(x))` would be more what you are looking for – Rich Scriven Dec 26 '14 at 21:13
  • A `dput(head(dates1))` would make it easier to provide assistance. – hrbrmstr Dec 27 '14 at 02:57

1 Answers1

1

As Richard Scriven notes, these look like Dates. No matter whether they are Dates or POSIXcts, this will likely be helpful:

foo <- table(as.Date(dates1))

This will of course only give you nonzero counts, which may not be what you want. If you want (possibly) zero counts for dates that do not appear in dates1, then convert it to a factor before tableing:

dates1 <- as.Date(dates1)
foo <- factor(dates1,levels=as.character(seq(min(dates1),max(dates1),by="day")))
table(foo)
Community
  • 1
  • 1
Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48