Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. When working with dates, I sometimes have counts, say of tweets from a certain account, that don't occur every day. When analyzing these data, it would be helpful to have every date present in my analysis data set, even those dates for which the count is zero.
  2. For some time, I didn't know how to do this. I tried:
  3.  
  4. ```{r}
  5. library(lubridate)
  6. mdy("1/1/2015") -> foo
  7. mdy("1/1/2016") -> bar
  8. foo:bar
  9. ```
  10.  
  11. which outputted a collection of 366 (consecutive) integers.
  12.  
  13. I then tried:
  14.  
  15. ```{r}
  16. mdy(foo:bar)
  17. ```
  18. which outputted 366 NAs, with a message that no origin value was specified.
  19.  
  20. Now, to get 366 dates, you can use `as_date` from `lubridate`:
  21.  
  22. ```{r}
  23. as_date(foo:bar)
  24. ```
  25. to get the consecutive 366 dates.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement