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
7
votes
1 answer

Using dplyr::if_else() in R to change the time zone of POSIXct timestamps based on value of another variable

I'm working with some timestamps in POSIXct format. Right now they are all showing up as being in the timezone "UTC", but in reality some are known to be in the "America/New_York" timezone. I'd like to correct the timestamps so that they all read as…
KTrooper
  • 495
  • 1
  • 4
  • 6
7
votes
1 answer

Difference in outputs using cumsum

Why are these two operations different? library(lubridate) library(magrittr) > seconds_to_period(1:1000) %>% cumsum %>% sum [1] 14492440 > 1:1000 %>% cumsum %>% sum [1] 167167000 I have seen, however, that the issue lies on the fact that cumsum…
Fustincho
  • 423
  • 2
  • 10
7
votes
1 answer

R lubridate ymd_hms millisecond diff

Is there something I'm missing in time conversions? Very simple example: library(lubridate) time <- "2019-01-14 10:58:23.438000" op <- options(digits.secs=6) ymd_hms(time, tz = "Europe/Helsinki") [1] "2019-01-14 10:58:23.437 EET" ymd_hms(time) [1]…
Hakki
  • 1,440
  • 12
  • 26
7
votes
4 answers

Collapse and merge overlapping time intervals

I am developing a tidyverse-based data workflow, and came across a situation where I have a data frame with lots of time intervals. Let's call the data frame my_time_intervals, and it can be reproduced like…
hpy
  • 1,989
  • 7
  • 26
  • 56
7
votes
1 answer

Generate a sequence of time using R and lubridate

Is there an efficient way to generate a time-sequence vector with tidyverse and lubridate? I know the two can work with seq() when one use the number of dates as the interval. For example, with the input: seq(today(), today()+dyears(1), 60) one can…
Carl H
  • 1,036
  • 2
  • 15
  • 27
7
votes
3 answers

extract weekdays from a set of dates in R

I know using the lubridate package, I can generate the respective weekday for each date of entry. I am now dealing with a large dataset having a lot of date entries and I wish to extract weekdays for each date entries. I think it is quite impossible…
Hindol Ganguly
  • 363
  • 1
  • 4
  • 16
7
votes
4 answers

R Lubridate Returns Unwanted Century When Given Two Digit Year

In R, I have a vector of strings representing dates in two different formats: "month/day/year" "month day, year" The first format has a two digit year so my vector looks something like this: c("3/18/75", "March 10, 1994", "10/1/80", "June 15,…
pseudorandom
  • 267
  • 5
  • 20
7
votes
5 answers

How to find next particular day?

I need to find all 'next friday' corresponding to a set of dates. For instance 2015-08-03 (Monday 3rd, August, 2015) as an input should return 2015-08-07 (Friday 7th, August, 2015) as an output. I could not find a way to manage this need while…
cho7tom
  • 1,030
  • 2
  • 13
  • 30
7
votes
2 answers

R: count days that start at sunset

I'm analysing temporal patterns in a complex data set consisting of several environmental variables as well as activity data from various animal species. These data have been collected by multiple experimental setups, and data from each setup have…
Yuri Robbers
  • 291
  • 1
  • 10
7
votes
2 answers

Why R package lubridate can't parse vector with multiple formats?

I'm using package lubridate to parse a vector of heterogeneously-formatted dates and convert them to string, like this: parse_date_time(c('12/17/1996 04:00:00 PM','4/18/1950 0130'), c('%m/%d/%Y %I:%M:%S %p','%m/%d/%Y %H%M')) This is the result: [1]…
Jesus Ramos
  • 149
  • 1
  • 8
7
votes
2 answers

changing POSIXct date vaules to first day of each week

I want to calculate the average Dist for each week using these data (below) while preserving the benefits of a using the POSIXct time class. df <- structure(list(IndID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,…
B. Davis
  • 3,391
  • 5
  • 42
  • 78
7
votes
4 answers

Aggregation by time period in lubridate

This question asks about aggregation by time period in R, what pandas calls resampling. The most useful answer uses the XTS package to group by a given time period, applying some function such as sum() or mean(). One of the comments suggested there…
Peter
  • 4,219
  • 4
  • 28
  • 40
6
votes
2 answers

Combine overlapping intervals with lubridate

I am looking to combine lubridate intervals such that if they overlap, take the min value from the internal first in time and the max value from the internal last in time and summarise to create a new interval that spans the entire period. Here is a…
boshek
  • 4,100
  • 1
  • 31
  • 55
6
votes
1 answer

Convert to local time zone from latitude and longitude R

I have one data frame with a lot of locations (around 30.000), and I need to convert the time of each location for the local time. I tried some ideas like this one, and this one. But they did not work for me. I have data like this: dt =…
6
votes
3 answers

Checking if there exists a value in vector of dates that lies within a given range

I have a vector of dates and want to check for a certain date, whether there exists a value in the vector within the 150 days before it, AND the 150 days before that. A simple example of my data looks like this: given_date <-…
bob
  • 610
  • 5
  • 23