lubridate

How to get week number and year from date in R

微笑、不失礼 提交于 2021-02-04 19:47:05
问题 I am trying to get both week number and year from a date object in R. Until then, I did it separately : I used isoweek() function to extract week and year() function to extract year. So that my data.frame has 3 variables : date, week, year This is working except for beginning/end of year : for example, 2015 has 53 weeks and January 1st, 2016 belongs to the 53rd week of 2015 ... but with my code, it is such that 1/1/2016 is week 53 but year 2016 whereas I would like it to be week 53 in year

Re-aggregating data - from coarse to finer temporal resolution

帅比萌擦擦* 提交于 2021-01-29 06:09:20
问题 I would like to follow-up on a question answered by @r2evans: Interpolation in R: retrieving hourly values. I am trying to re-aggregate 3-hr data into hourly. If I use the following small reproducible dataset ("tair"): tair<-structure(list(Year = c(1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L), Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), DoY = c(1L,1L, 1L, 1L, 1L, 1L, 1L, 2L), Hour = c(3L, 6L, 9L, 12L, 15L, 18L, 21L, 0L), Kobb = c(3.032776, 3.076996, 3.314209, 1.760345, 1.473724,1

categorize based on date ranges in R

这一生的挚爱 提交于 2021-01-28 07:55:26
问题 How do I categorize each row in a large R dataframe (>2 million rows) based on date range definitions in a separate, much smaller R dataframe (12 rows)? My large dataframe, captures, looks similar to this when called via head(captures) : id date sex 1 160520 2016-11-22 1 2 1029735 2016-11-12 1 3 1885200 2016-11-05 1 4 2058366 2015-09-26 2 5 2058367 2015-09-26 1 6 2058368 2015-09-26 1 My small dataframe, seasons, looks similar to this in its entirety: Season Opening.Date Closing.Date 2016 2016

Collapse and merge overlapping time intervals

大城市里の小女人 提交于 2021-01-21 09:43:59
问题 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 this: library(tidyverse) library(lubridate) my_time_intervals <- tribble( ~id, ~group, ~start_time, ~end_time, 1L, 1L, ymd_hms("2018-04-12 11:15:03"), ymd_hms("2018-05-14 02:32:10"), 2L, 1L, ymd_hms("2018-07-04 02:53:20"), ymd_hms("2018-07-14 18:09:01"), 3L, 1L, ymd_hms("2018-05-07 13:02

Change Date Format from %y-%m-%d %h:%m:%s to %Y%M%D with lubridate and mutate

天大地大妈咪最大 提交于 2021-01-03 04:09:41
问题 I've got a tbl_df with two columns StartTime and StopTime . Both are dttm . I want to change its format from "%y-%m-%d %h:%m:%s" to "%y%m%d" . I've tried data <- mutate(data, StartTime = ymd(StartTime), StopTime = ymd(StopTime)) But it returns Warning messages: 1: All formats failed to parse. No formats found. 2: All formats failed to parse. No formats found. How can I do it? Please, don't send other questions that don't use lubridate package. Thanks 回答1: I think this should work library

Change Date Format from %y-%m-%d %h:%m:%s to %Y%M%D with lubridate and mutate

冷暖自知 提交于 2021-01-03 04:06:29
问题 I've got a tbl_df with two columns StartTime and StopTime . Both are dttm . I want to change its format from "%y-%m-%d %h:%m:%s" to "%y%m%d" . I've tried data <- mutate(data, StartTime = ymd(StartTime), StopTime = ymd(StopTime)) But it returns Warning messages: 1: All formats failed to parse. No formats found. 2: All formats failed to parse. No formats found. How can I do it? Please, don't send other questions that don't use lubridate package. Thanks 回答1: I think this should work library

Change Date Format from %y-%m-%d %h:%m:%s to %Y%M%D with lubridate and mutate

拥有回忆 提交于 2021-01-03 04:00:42
问题 I've got a tbl_df with two columns StartTime and StopTime . Both are dttm . I want to change its format from "%y-%m-%d %h:%m:%s" to "%y%m%d" . I've tried data <- mutate(data, StartTime = ymd(StartTime), StopTime = ymd(StopTime)) But it returns Warning messages: 1: All formats failed to parse. No formats found. 2: All formats failed to parse. No formats found. How can I do it? Please, don't send other questions that don't use lubridate package. Thanks 回答1: I think this should work library

Change Date Format from %y-%m-%d %h:%m:%s to %Y%M%D with lubridate and mutate

我怕爱的太早我们不能终老 提交于 2021-01-03 03:56:12
问题 I've got a tbl_df with two columns StartTime and StopTime . Both are dttm . I want to change its format from "%y-%m-%d %h:%m:%s" to "%y%m%d" . I've tried data <- mutate(data, StartTime = ymd(StartTime), StopTime = ymd(StopTime)) But it returns Warning messages: 1: All formats failed to parse. No formats found. 2: All formats failed to parse. No formats found. How can I do it? Please, don't send other questions that don't use lubridate package. Thanks 回答1: I think this should work library

Is it possible to print a duration with HH:MM:SS format?

让人想犯罪 __ 提交于 2020-12-31 06:20:48
问题 t1<-as.POSIXct("2017-03-02 11:58:20") t2<-as.POSIXct("2017-03-02 12:00:05") print(lubridate::as.duration(lubridate::interval(t1,t2))) [1] "105s (~1.75 minutes)" Is it possible to have the duration expressed in HH:MM:SS? So in this case it would be: 00:01:45 回答1: You can use seconds_to_period from lubridate with sprintf library(lubridate) td <- seconds_to_period(difftime(t2, t1, units = "secs")) sprintf('%02d:%02d:%02d', td@hour, minute(td), second(td)) #[1] "00:01:45" This is based on an