lubridate
extract hours and seconds from POSIXct for plotting purposes in R
This is a good question, and highlights some of the difficulty in dealing with dates in R. The lubridate package is very handy, so below I present two approaches, one using base (as suggested by @RJ-) and the other using lubridate. Recreate the (first two rows of) the dataframe in the original post: foo <- … Read more
Create a Vector of All Days Between Two Dates
You’re looking for seq > seq(as.Date(“2011-12-30”), as.Date(“2012-01-04″), by=”days”) [1] “2011-12-30” “2011-12-31” “2012-01-01” “2012-01-02” “2012-01-03” [6] “2012-01-04” Or, you can use : > as.Date(as.Date(“2011-12-30”):as.Date(“2012-01-04″), origin=”1970-01-01”) [1] “2011-12-30” “2011-12-31” “2012-01-01” “2012-01-02” “2012-01-03” [6] “2012-01-04″ Note that with : “Non-numeric arguments are coerced internally”. Thus, we convert back to class Date, using as.Date method for class ‘numeric’ and … Read more
R sequence of dates with lubridate
ymd is a wrapper to parse date strings and returns a POSIXct object. You simply need to use standard terminology described in ?seq.POSIXt (not lubridate) to define weeks seq(ymd(‘2012-04-07’),ymd(‘2013-03-22’), by = ‘1 week’) seq(ymd(‘2012-04-07’),ymd(‘2013-03-22’), by = ‘weeks’) will works as will seq(ymd(‘2012-04-07’),ymd(‘2013-03-22’), by = ‘2 week’) You could coerce the lubridate Period class object to … Read more