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
9
votes
3 answers

How to format a difftime object to a string with HH:MM:SS

I think this is quite a simple question, I am however incapable of solving it, so any help would be greatly appreciated. I have a difftime object, generated as follows: > duration <- difftime(end_time, start_time) > duration Time difference of…
nikUoM
  • 639
  • 1
  • 8
  • 18
7
votes
1 answer

How to get difference (in minutes) between two date strings?

I have two dates: date1 <- '2015-10-05T20:57:00.000' date2 <- '2015-10-05T22:43:00.000' 1) I need to know the difference in minutes 2) Also I want to extract year, month, day and hour. This is how I tried to solve these tasks: 1) time <-…
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
6
votes
3 answers

How do you extract the time unit when using difftime()?

I'm currently running a for loop code and at the end of each loop I am measuring the duration of the loop and print a message that tells the user how long did the loop take. To get the duration I'm using duration <- difftime(end_time, start_time)…
Alyssa
  • 63
  • 5
5
votes
3 answers

difftime with previous non-NA value from other columns

I've got a dataframe of 3 variables: POSIXct object - time, numeric - RRR and factor - he. Where RRR is an amount of liquid precipitation and he is the hydrological event number, here its time corresponds to the beginning of the flood event. df <-…
atsyplenkov
  • 1,158
  • 13
  • 25
5
votes
3 answers

Grouped mean of difftime fails in data.table

Preface: I have a column in a data.table of difftime values with units set to days. I am trying to create another data.table summarizing the values with dt2 <- dt[, .(AvgTime = mean(DiffTime)), by = Group] When printing the new data.table, I see…
Gaffi
  • 4,307
  • 8
  • 43
  • 73
5
votes
4 answers

How to calculate time differences in C++ with time_t before the epoch?

What I would like to do with my simple program is to calculate a difference in seconds between two dates. time_t referenceDate; time_t dateNow = time(0); struct tm referenceDateComponent = {0}; referenceDateComponent.tm_hour =…
BEPP1
  • 945
  • 2
  • 11
  • 15
4
votes
4 answers

How to select for rows in a dataframe that have a time within 30 minutes of another dataframe

I have 2 datasets that looks like the following (with about 600 total entries for both) Dataframe1 name A_time measurement_A A 6/10/22 7:22 3.4 A 6/10/22 16:15 4.4 A 6/11/22 6:15 5.5 A 6/11/22 15:44 6.6 B 5/20/22 6:30 7.6 B 5/20/22…
ol1975
  • 51
  • 3
4
votes
3 answers

Get the time difference from time varaibles stored as character

I have two character variables in my dataframe start_time and stop_time: start_time stop_time 1 19:5:00 19:11:00 2 20:33:37 20:34:39 3 20:23:00 20:23:38 4 20:12:00 20:13:00 5 …
nenno lillo
  • 537
  • 1
  • 4
  • 12
4
votes
3 answers

How to format difftime as hh:mm in ggplot2?

I want to display difftime data with ggplot2 and I want the tick format to be hh:mm. library(ggplot2) a= as.difftime(c("0:01", "4:00"), "%H:%M", unit="mins") b= as.difftime(c('0:01', "2:47"), "%H:%M", unit="mins") ggplot(data=NULL, aes(x=b,…
peer
  • 4,171
  • 8
  • 42
  • 73
3
votes
3 answers

Age groups into monthly buckets

I'm struggling to find a solution for the following problem. I have a df with id's/ dob's and another monthbucket df as following set.seed(33) df <- data.frame(dob = sample(seq(as.Date('1940/01/01'), as.Date('2010/01/01'), by="day"), 10), …
CER
  • 854
  • 10
  • 22
3
votes
1 answer

How to calculate number of days between two dates in one column in R

I have the following two data frames: Date <- seq(as.Date("2013/1/1"), by = "day", length.out = 17) x <-data.frame(Date) x$discharge <-…
Matt
  • 323
  • 2
  • 12
3
votes
1 answer

difftime in R generate NA values

as.numeric(difftime( strptime("12:00 AM", "%I:%M %p" ), strptime("2:00 AM", "%I:%M %p" ), units='hours')) result: NA. I am trying to calculate the time difference, but some rows generate NA values which are really annoying, something wrong here?
DatascienceGeeks
  • 368
  • 2
  • 12
3
votes
1 answer

Diff for POSIXct with units, like in difftime

I have got a vector of POSIXct-data and want to calculate the difference between consecutive elements, like it is done by diff e.g: burst <- as.POSIXct(c("2016-11-07 17:20:52","2016-11-07 16:21:52", "2016-11-07 15:21:52", "2016-11-02…
barracuda317
  • 608
  • 7
  • 24
3
votes
2 answers

R "difftime" exhibits strange behavior

I am a bit confused by the function "difftime". When I calculate the time difference up to 27 March 2016 everything is ok. But as soon as I try 28, 29, 30 or 31 March 2016 there seems to be a problem: > difftime("2016-03-27","1979-01-01",…
user3270948
  • 35
  • 1
  • 4
2
votes
2 answers

R difftime output different depending on input formats (as.character() wrapper vs without)

example data: test <- structure(list(date1 = structure(c(1632745800, 1632745800), tzone = "UTC", class = c("POSIXct", "POSIXt")), date2 = structure(c(1641468180, 1641468180), tzone = "UTC", class = c("POSIXct", "POSIXt"))), row.names = c(NA, -2L),…
vizidea
  • 153
  • 7
1
2 3
12 13