Questions tagged [difftime]

A function in R that calculates the difference of two times.

A function in that calculates the difference of two times.

is a function in that provides creation, printing and some arithmetic for calculating time intervals or time differences.

The signature of is as follows:

difftime(time1, time2, tz, units = c("auto", "secs", "mins", "hours", "days", "weeks"))

See official documentation for more details.

181 questions
1
vote
3 answers

Last value manually inputted in an r script difftime output

R coding in Databricks. I want the time intervals in hours between date1 entries, arranged by pid, med and date1. I want the latest date1 entry in a sequence of events per date to be manually adjustable to 24 hours. A cohort is when pid, med and…
db2020
  • 69
  • 8
1
vote
0 answers

label is removed from difftime variables after dplyr::filter

When I dplyr::filter a data.frame with labels, the labels remain but not for variables of class difftime: library(tidyverse) library(sjlabelled) df <- data.frame(a = c(1, 2, 3), start = c("2009-10-16", …
user63230
  • 4,095
  • 21
  • 43
1
vote
1 answer

I have 2 kinds of posts (sponsored and non sponsored) for X users. I want to know for each post of a user when the last sponsored post happened (days)

I have 2 kinds of posts (sponsored and non sponsored) for many users. I know the date when each post happened. Now I need to know for each post of a user when the last sponsored post happened (in full days) see the column "Days since last…
1
vote
1 answer

How do I calculate age as years, months, and days in R?

I want to calculate age as years, months, and days. I can do it with python's dateutil package from dateutil.relativedelta import relativedelta from datetime import date rel_obj = relativedelta(date(2008,8,6),date(2004,1,1)) rel_obj.years,…
1
vote
2 answers

R - weird result from difftime function

I was using the difftime function from the base package in R and based on my data I found a couple of weird return values of this function: > difftime("2014-10-29", "2014-10-21", units = "days") Time difference of 8.041667 days >…
Marius
  • 990
  • 1
  • 14
  • 34
1
vote
1 answer

Calculating numbers of week for a specific time period

Hi I have this data frame which contains the start and end dates of healthcare services. It looks like this: id <- c("A", "B", "B", "C", "D") start_date <- c("2014-08-18", "2014-08-04", "2016-10-10", "2016-04-01", "2015-12-31") end_date <-…
j1234
  • 93
  • 7
1
vote
1 answer

Remove rows that are within 12 months of each other for each participant across 15 years

I am new to R. I have a large dataset that goes over 15 years (2007-2022) (see below an example portion). Essentially, I want to create a 12-month rolling window and remove the rows within 12 months of the first collection date for each subject. For…
Jess H
  • 39
  • 7
1
vote
0 answers

difftime returns 0 in C++

I am trying to calculate the elapsed time between the date a book was borrowed and today, but difftime always returns 0. This is what I have : #include #include #include #include #include #include…
1
vote
1 answer

What is the most efficient way to calculate difftime of two time which are string format

I have a large data set (as a data.table,more than 100 million rows) and wish to calculate the difftime between two column.and the time string format if "%H%M%OS",without leading zeros.eg,data ID time0 time1 1 93005220 93005320 2 93551060…
A.hu
  • 27
  • 4
1
vote
1 answer

Issues with R rounding difftime in a for loop

Running the following to test how fast runif() runs: start_time <- Sys.time() runif(1) end_time <- Sys.time() run_times= as.numeric(as.difftime(end_time - start_time, units ="secs")) It kicks out a fraction of a second for run_times…
1
vote
0 answers

Successive time difference with POSIXct object in specific unit

I'd like to compute the successive time differences of a vector of date-time (POSIXct object) in a specific unit in R. With difftime, there is an argument units to force the units. But that's only for two date-times. For successive differences, I…
user3631369
  • 329
  • 1
  • 12
1
vote
1 answer

How is it possible that tv_sec of both timeval structures are the same but subtracting them gives me a non zero value

timeval end_time; timeval Dbg_timer; The above time structures where initialized with gettimeofday() at some point. Below is how I calculated the time difference: long int elaspsed_time_s = end_time.tv_sec -…
African_king
  • 177
  • 1
  • 8
1
vote
3 answers

calculate the time difference. R

I have a a dataframe with 2 column: StartTime EndTime 1. 2019-05-06 08:34:15 2019-05-06 08:48:30 2. 2019-05-06 10:07:18 2019-05-06 10:21:34 3. 2019-05-06 15:13:10 2019-05-06 15:27:26 4. 2019-05-06 15:35:52 2019-05-06…
ken
  • 11
  • 2
1
vote
2 answers

For loop with dplyr pipeline: problem using dynamic and date variables correctly

I have the below code and example data. I have two issues: The name of the new variable created using mutate appears as "New_var" in the corresponding data frames rather than the character string(e.g., df1_timediff) that I have assigned to it…
Aepkr
  • 101
  • 8
1
vote
3 answers

Same function outputs different results when calculating difference between two timestamps

I'm currently facing a weird issue where the same function outputs a different result. The function is supposed to calculate the time difference between a provided date and the current time. Since this function is supposed to work with milliseconds,…
Louis Bernard
  • 229
  • 4
  • 20