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
6
votes
5 answers

ifelse() function - refer to the following day

I have a dataframe with 2 columns: the date and the return. df <- tibble( date = lubridate::today() +0:9, return= c(1,2.5,2,3,5,6.5,1,9,3,2)) And now I want to add a third column with an ifelse-condition. If the return on day t is higher than 3.5,…
TobKel
  • 1,293
  • 8
  • 20
6
votes
3 answers

Counting days of the year with leap years

I am currently trying to code this in R. I would like to take a date that I have in %Y-%m-%d (ex: 2017-12-31) format and convert it to the day of the year. However, I would like it to always count 02/28 as day #59 and 03/01 as day #61. When it is…
Sarah
  • 411
  • 4
  • 14
6
votes
0 answers

How to get the exact season (autumn, winter, spring and summer) from a datetime vector in R using some built-in package function?

I need to get the exact season (autumn, winter, spring and summer) of my dataset using a "POSIXct" "POSIXt" datetime column. It's important to consider that the seasons depends on equinoxes and solstices on Earth and the exact time it takes places…
Guz
  • 387
  • 3
  • 21
6
votes
4 answers

How to convert date to the closest weekend (Saturday)

I have a data frame with Date in the "%d-%m-%Y" format and have the week number. The dates are weekdays and I want the the Saturday for that week in another column. I had initially check whether the date is a weekday or weekend using function in the…
Ami
  • 197
  • 1
  • 12
6
votes
1 answer

Joining data frames by lubridate date %within% intervals

I've been practicing and learning wrangling R data frames with columns that contain lubridate data types, such as an example problem in my other question. Now, I am trying to do the equivalent of joining two data frames, but joining them by whether…
hpy
  • 1,989
  • 7
  • 26
  • 56
6
votes
2 answers

R lubridate package date-time creation omits time at midnight

I am trying to create date-time using lubridate package, and I've tried the following values: library(lubridate) ymd_hms("2017-07-02 23:00:00") [1] "2017-07-02 23:00:00 UTC" ymd_hms("2017-07-02 00:00:00") [1] "2017-07-02 UTC" It seems that if you…
ACuriousCat
  • 1,003
  • 1
  • 8
  • 21
6
votes
2 answers

Extract Fiscal Year with R Lubridate

I'll create several dates. library(lubridate) x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31")) I can extract the year and quarter from these x values. quarter(x, with_year = TRUE, fiscal_start = 10) [1] 2012.2 2012.3 2012.4…
stackinator
  • 5,429
  • 8
  • 43
  • 84
6
votes
2 answers

How to extract Month and Day from Date in R and convert it as a date type?

I am Rstudio for my R sessions and I have the following R codes: d1 <- read.csv("mydata.csv", stringsAsFactors = FALSE, header = TRUE) d2 <- d1 %>% mutate(PickUpDate = ymd(PickUpDate)) str(d2$PickUpDate) output of last line of code above…
user3115933
  • 4,303
  • 15
  • 54
  • 94
6
votes
2 answers

Vectorised time zone conversion with lubridate

I have a data frame with a column of date-time strings: library(tidyverse) library(lubridate) testdf = data_frame( mytz = c('Australia/Sydney', 'Australia/Adelaide', 'Australia/Perth'), mydt = c('2018-01-17T09:15:00', '2018-01-17T09:16:00',…
jimjamslam
  • 1,988
  • 1
  • 18
  • 32
6
votes
3 answers

Error in as.POSIXlt.POSIXct(x, tz) : (converted from warning) unknown timezone 'GMT'

I find myself confronted with a problem for which I fail to find a clue to a solution - which makes it very hard to actually deal with it.. Here goes: In R (with R-Studio) I try to create a Date object with the library…
Taeke
  • 179
  • 5
6
votes
1 answer

lubridate: how to parse month-year?

I have a column of dates as follows, > mymonth = c('10/2015','11/2016','12/2016') > data <- data_frame(mymonth) > data # A tibble: 3 × 1 mymonth 1 10/2015 2 11/2016 3 12/2016 Here, obviously, my month corresponds to a particular month…
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
6
votes
1 answer

How can I add fractional times in R?

I have large number of dates in this format: dt = as.POSIXct("2004-04-02 12:45:00 UTC") And I have to add/subtract numbers that may not always be whole numbers.I am using lubridate library. Example: dt - days(2) [1] "2004-03-31 12:45:00 UTC" But,…
maximusdooku
  • 5,242
  • 10
  • 54
  • 94
6
votes
1 answer

how can I sum or average hours in R

Why does sum and average do not work on hours extracted using lubridate, when simple addition using + and division works individually. Dataset Category Time1 Time2 hr1 hr2 1 A 0:30:00 24:00:00 30M 0S 24H 0M 0S 2 …
Shoaibkhanz
  • 1,942
  • 3
  • 24
  • 41
6
votes
1 answer

Using lubridate and ggplot2 effectively for date axis

Consider this example: library(ggplot2) library(lubridate) set.seed(4) date <- seq(from = as.POSIXct("2012-01-01"), to = as.POSIXct("2014-12-31"), by = "days") value <- c(rnorm(274, 50, 1), rnorm(274, 55, 1), rnorm(274, 55, 2), rnorm(274, 60,…
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
6
votes
3 answers

add 1 business day to date in R

I have a Date object in R and would like to add 1 business day to this date. If the result is a holiday, I would like the date to be incremented to the next non-holiday date. Let's assume I mean NYSE holidays. How can I do this? Example: mydate =…
Alex
  • 19,533
  • 37
  • 126
  • 195