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

2520 questions
136
votes
6 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
  • 7,084
  • 12
  • 57
  • 87
100
votes
3 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,848
  • 14
  • 49
  • 75
96
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
  • 17,277
  • 8
  • 49
  • 70
70
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,965
  • 8
  • 36
  • 37
58
votes
4 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
  • 805
  • 1
  • 9
  • 20
55
votes
4 answers

Convert date-time string to class Date

I have a data frame with a character column of date-times. When I use as.Date, most of my strings 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
  • 38,798
  • 44
  • 129
  • 168
45
votes
3 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
  • 10,244
  • 19
  • 61
  • 99
42
votes
6 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
  • 7,646
  • 16
  • 60
  • 113
40
votes
3 answers

Convert two-digit years to four-digit years with correct century

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
  • 91,525
  • 24
  • 209
  • 285
34
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
  • 985
  • 2
  • 14
  • 28
33
votes
5 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
  • 6,328
  • 5
  • 33
  • 47
30
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,738
  • 10
  • 42
  • 54
29
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,973
  • 10
  • 55
  • 89
28
votes
4 answers

How to aggregate a dataframe by week?

Consider the following example library(tidyverse) library(lubridate) time <- seq(from =ymd("2014-02-24"),to= ymd("2014-03-20"), by="days") set.seed(123) values <- sample(seq(from = 20, to = 50, by = 5), size = length(time), replace = TRUE) df2 <-…
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
28
votes
3 answers

Simplest way to extract date from timestamp

Consider the following timestamp timestamp <- ymd_hms("2011-08-10 14:00:00", tz = "Pacific/Auckland") > timestamp [1] "2011-08-10 14:00:00 NZST" What is the simplest way to get the day part 2011-08-10 from it, and making sure this day is a proper…
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
1
2 3
99 100