Questions tagged [lubridate]

lubridate is an R package that makes it easier to work with dates and time objects.

The lubridate package facilitates working with dates and times in . lubridate also expands the type of mathematical operations that can be performed with date-time objects. It introduces three new time span classes borrowed from :

  • durations, which measure the exact amount of time between two points
  • periods, which accurately track clock times despite leap years, leap seconds, and day light savings time
  • intervals, a protean summary of the time information between two points

Repositories

Vignettes

Other resources

Related tags

1839 questions
79
votes
2 answers

R sequence of dates with lubridate

Hi I'm trying to get a sequence of dates with lubridate This doesn't work seq(ymd('2012-04-07'),ymd('2013-03-22'),by=week(1)) the base command seq(as.Date('2012-04-7'),as.Date('2013-03-22'),'weeks') does, but I'd like to know if there is an…
Tahnoon Pasha
  • 5,196
  • 12
  • 42
  • 71
77
votes
3 answers

How to convert Excel date format to proper date in R

I'm working with a csv which unfortunately has logged datetimes using the number format of 42705 although it should be 01/12/2016. I'd like to convert it to the right format in R using lubridate or some other package. Is there a function that will…
elksie5000
  • 4,829
  • 8
  • 44
  • 71
70
votes
3 answers

Create a Vector of All Days Between Two Dates

Is there an easy way in R for me to itemize all valid days that occurred between two specified dates? For instance, I'd like the following inputs: itemizeDates(startDate="12-30-11", endDate="1-4-12") To produce the following dates: "12-30-11"…
Jeff Allen
  • 15,749
  • 8
  • 45
  • 67
64
votes
5 answers

extract hours and seconds from POSIXct for plotting purposes in R

Suppose I have the following data.frame foo start.time duration 1 2012-02-06 15:47:00 1 2 2012-02-06 15:02:00 2 3 2012-02-22 10:08:00 3 4 2012-02-22 09:32:00 4 5 2012-03-21 13:47:00 5 And class(foo$start.time)…
andrewj
  • 2,705
  • 6
  • 31
  • 36
43
votes
2 answers

Generate a sequence of the last day of the month over two years

I use lubridate and figured that this would be so easy ymd("2010-01-31")+months(0:23) But look what one gets. It is all messed up! [1] "2010-01-31 UTC" "2010-03-03 UTC" "2010-03-31 UTC" "2010-05-01 UTC" "2010-05-31 UTC" "2010-07-01 UTC"…
Farrel
  • 9,584
  • 19
  • 57
  • 95
43
votes
5 answers

How to extract Month from date in R

I am using the lubridate package and applying the month function to extract month from date. I ran the str command on date field and I got Factor w/ 9498 levels "01/01/1979","01/01/1980",..: 5305 1 1 1 1 1 1 1 1 1 ... >…
apTNow
  • 655
  • 1
  • 7
  • 19
41
votes
3 answers

Convert character to class Date

I have a data frame with a character column of dates. When I use as.Date, most of my dates are parsed correctly, except for a few instances. The example below will hopefully show you what is going on. # my attempt to parse the string to Date --…
Btibert3
  • 34,187
  • 40
  • 119
  • 164
38
votes
3 answers

Is there a more elegant way to convert two-digit years to four-digit years with lubridate?

If a date vector has two-digit years, mdy() turns years between 00 and 68 into 21st Century years and years between 69 and 99 into 20th Century years. For example: library(lubridate) mdy(c("1/2/54","1/2/68","1/2/69","1/2/99","1/2/04")) gives…
eipi10
  • 81,881
  • 20
  • 176
  • 248
29
votes
1 answer

Valid time zones in lubridate

A quick google search seems to get me nowhere. What are valid time zones in lubridate's tz option? In particular, am looking for Brasilia's time zone. Thanks! library(lubridate) dts <- c("6-3-1995 12:01:01","29-3-1995 23:01:01","29-3-1995…
emagar
  • 813
  • 1
  • 11
  • 23
27
votes
4 answers

First day of the month from a POSIXct date time using lubridate

Given a POSIXct date time, how do you extract the first day of the month for aggregation? library(lubridate) full.date <- ymd_hms("2013-01-01 00:00:21")
nacnudus
  • 5,420
  • 5
  • 30
  • 46
26
votes
4 answers

Floor a year to the decade in R

I would like to floor a set of dates to the nearest decade, e.g: 1922 --> 1920, 2099 --> 2090, etc. I was hoping I could do this in Lubridate, as in: floor_date(1922, 'decade') But I get: Error in match.arg(unit) : 'arg' should be…
Monica Heddneck
  • 2,967
  • 5
  • 41
  • 77
25
votes
2 answers

Add/subtract 6 months (bond time) in R using lubridate

I am looking to add and subtract six months reliably with lubridate. For example, adding six months to 12/31/2014 should result in 6/30/2015, and adding to 2/28/2014 should result in 8/31/2014 The issue with as.Date("2014-12-31") + months(6), is…
Michael Clinton
  • 605
  • 1
  • 6
  • 12
25
votes
3 answers

R lubridate converting seconds to date

I have a simple question regarding R's lubridate package. I've a series of timestamps in seconds since epoch. I want to convert this to YYYY-MM-DD-HH format. In base R, I can do something like this to first convert it to a date format > x =…
broccoli
  • 4,316
  • 8
  • 33
  • 50
24
votes
7 answers

How to determine if date is a weekend or not (not using lubridate)

I have a vector of date objects (yyyy-mm-dd) and I want to determine if any of them are on weekend or not. Is there a function that can determine this straightaway? I can use wday() in the lubridate package and then determine if returned value is…
DonDyck
  • 1,353
  • 4
  • 18
  • 35
23
votes
4 answers

Time difference in years with lubridate?

I would like to use lubridate to calculate age in years given their date of birth and today's date. Right now I have this: library(lubridate) today<-mdy(08312015) dob<-mdy(09071982) today-dob which gives me their age in days.
Ignacio
  • 6,566
  • 8
  • 51
  • 99
1
2 3
99 100