0

Consider a table of time-frequency

2008-01-01 2008-01-03 2008-01-05 2008-01-06 2008-01-07 2008-01-08
         1          8          2          2          3          1

Note that 2008-01-02 and 2008-01-04 are lost.

The expected result looks like this:

2008-01-01 2008-01-02 2008-01-03 2008-01-04 2008-01-05 2008-01-06 2008-01-07 2008-01-08
         1          0          8          0          2          2          3          1

Or in the form of a date frame:

   Time    Frequency
2008-01-01    1
2008-01-02    0
2008-01-03    8
2008-01-04    0
2008-01-05    2
2008-01-06    2
2008-01-07    3
2008-01-08    1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user7453767
  • 339
  • 2
  • 14

1 Answers1

2

A better option would be to convert to factor with levels specified and then do the table

rndates <- range(dates)
table(factor(dates, levels = as.character(seq(rndates[1], rndates[2], by = "1 day"))))
#  2008-01-01 2008-01-02 2008-01-03 2008-01-04 2008-01-05 2008-01-06 2008-01-07 2008-01-08 
#        4          0          7          0          1          5          6          2 

data

set.seed(24)
dates <- sample(as.Date(c("2008-01-01", "2008-01-03", "2008-01-05", 
    "2008-01-06", "2008-01-07", "2008-01-08")), 25, replace = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662