lubridate

Get the range and random days within that range using R

*爱你&永不变心* 提交于 2020-07-23 06:18:24
问题 I have a data frame like as shown below test_df <- data.frame("subbject_id" = c(1,2,3,4,5), "date_1" = c("01/01/2003","12/31/2007","12/30/2008","01/02/2007","01/01/2007")) test_df = test_df %>% mutate(date_1 = mdy(date_1), previous_year = floor_date(date_1, 'year'), next_year = ceiling_date(date_1, 'year') - 1, days_to_previous_year = as.integer(date_1 - previous_year), days_to_next_year = as.integer(next_year - date_1), rand_days_prev_year = sample.int(days_to_previous_year, 1), rand_days

Creating a time series in R with a row every 15 minutes

强颜欢笑 提交于 2020-06-29 13:13:57
问题 I'm having trouble creating a time series (POSIXct or dttm column) with a row every 15 minutes. Something that will look like this for every 15 minutes between Jan 1st 2015 and Dec 31st 2016 (here as month/day/year hour:minutes): 1/15/2015 0:00 1/15/2015 0:15 1/15/2015 0:30 1/15/2015 0:45 1/15/2015 1:00 A loop starting date of 01/01/2015 0:00 and then adding 15 minutes until 12/31/2016 23:45? Does anyone has an idea of how this can be done easily? Thanks! 回答1: Little bit easier to read

Creating a time series in R with a row every 15 minutes

最后都变了- 提交于 2020-06-29 13:12:02
问题 I'm having trouble creating a time series (POSIXct or dttm column) with a row every 15 minutes. Something that will look like this for every 15 minutes between Jan 1st 2015 and Dec 31st 2016 (here as month/day/year hour:minutes): 1/15/2015 0:00 1/15/2015 0:15 1/15/2015 0:30 1/15/2015 0:45 1/15/2015 1:00 A loop starting date of 01/01/2015 0:00 and then adding 15 minutes until 12/31/2016 23:45? Does anyone has an idea of how this can be done easily? Thanks! 回答1: Little bit easier to read

Joining data frames by lubridate date %within% intervals

戏子无情 提交于 2020-06-25 18:10:45
问题 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 one timestamp in one data frame falls within an interval in the other data frame. For example: This is df1 : > glimpse(df1) Observations: 6,160 Variables: 4 $ upload_id <int> 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, ... $ site_id

how to transform time codes into turn codes

放肆的年华 提交于 2020-06-17 06:29:37
问题 I want to transform time codes like these library(lubridate) library(tidyverse) df_time <- tibble(time = c(ymd_hms("2020_01_01 00:00:01"), ymd_hms("2020_01_01 00:00:02"), ymd_hms("2020_01_01 00:00:03"), ymd_hms("2020_01_01 00:00:04"), ymd_hms("2020_01_01 00:00:05"), ymd_hms("2020_01_01 00:00:06")), a = c(0, 1, 1, 1, 1, 0), b = c(0, 0, 1, 1, 0, 0)) resulting in >df_time # A tibble: 6 x 3 time a b <dttm> <dbl> <dbl> 1 2020-01-01 00:00:01 0 0 2 2020-01-01 00:00:02 1 0 3 2020-01-01 00:00:03 1 1 4

R subtracting 1 month from today's date gives NA

会有一股神秘感。 提交于 2020-04-27 06:03:12
问题 I have a script in which I subset my data according to some set time periods and wanted to subset all the records that had occurred in the last month. However if I try to subtract one month from today's date it yields an NA: > today <- Sys.Date() > today [1] "2017-03-29" > today - months(1) [1] NA I do have lubridate loaded but I think this calculation is being performed with base R. If I subtract 2 or more months it works fine: > today - months(2) [1] "2017-01-29" > today - months(3) [1]

R subtracting 1 month from today's date gives NA

狂风中的少年 提交于 2020-04-27 06:02:05
问题 I have a script in which I subset my data according to some set time periods and wanted to subset all the records that had occurred in the last month. However if I try to subtract one month from today's date it yields an NA: > today <- Sys.Date() > today [1] "2017-03-29" > today - months(1) [1] NA I do have lubridate loaded but I think this calculation is being performed with base R. If I subtract 2 or more months it works fine: > today - months(2) [1] "2017-01-29" > today - months(3) [1]

R subtracting 1 month from today's date gives NA

喜你入骨 提交于 2020-04-27 06:01:48
问题 I have a script in which I subset my data according to some set time periods and wanted to subset all the records that had occurred in the last month. However if I try to subtract one month from today's date it yields an NA: > today <- Sys.Date() > today [1] "2017-03-29" > today - months(1) [1] NA I do have lubridate loaded but I think this calculation is being performed with base R. If I subtract 2 or more months it works fine: > today - months(2) [1] "2017-01-29" > today - months(3) [1]

convert invalid date to a valid date like value using R

房东的猫 提交于 2020-03-16 08:10:52
问题 I have a dataframe like as shown below d1 <- c('21/01/2052 9:02','12/30/2042 9:25','10134-5-28 0:0:0','10109-12-27 0:0:0','10176-6-7 0:0:0') d2 <- c('22/03/2052 9:22','31/10/2043 9:25','10134-6-02 0:0:0','10109-12-30 0:0:0','10176-6-9 0:0:0') df_test <- c(d1,d2) Input I tried something like below using lubridate package but doesn't seem to help mdy_hm(df_test$d1) This resulted in a warning message and resulted in NAs for invalid values It's fine if invalid dates can't be processed and returns

convert invalid date to a valid date like value using R

依然范特西╮ 提交于 2020-03-16 08:10:20
问题 I have a dataframe like as shown below d1 <- c('21/01/2052 9:02','12/30/2042 9:25','10134-5-28 0:0:0','10109-12-27 0:0:0','10176-6-7 0:0:0') d2 <- c('22/03/2052 9:22','31/10/2043 9:25','10134-6-02 0:0:0','10109-12-30 0:0:0','10176-6-9 0:0:0') df_test <- c(d1,d2) Input I tried something like below using lubridate package but doesn't seem to help mdy_hm(df_test$d1) This resulted in a warning message and resulted in NAs for invalid values It's fine if invalid dates can't be processed and returns