lubridate

Elegant way to filter records based on multiple criteria using R

ぐ巨炮叔叔 提交于 2020-08-26 07:15:07
问题 I have a data frame like as shown below test_df <- data.frame("subject_id" = c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), "date_1" = c("01/01/2003", "12/31/2007", "12/30/2008", "12/31/2005", "01/01/2007", "01/01/2013", "12/31/2008", "03/04/2006", "12/31/2009", "01/01/2015", "01/01/2009")) What I would like to do is Arrange the dates in ascending order for each subject (sort asc within groups) Remove date records for each subject based on below criteria (year doesn't matter): 2a. remove only Dec 31st

R: Determine if each date interval overlaps with all other date intervals in a dataframe

旧街凉风 提交于 2020-08-25 03:22:11
问题 For each date interval row in my dataframe, I would like to determine whether it overlaps with all other date intervals or not. Excluding itself. A dataframe with start and end date, representing intervals: `data <- read.table(header=TRUE,text=" start.date end.date 2019-09-01 2019-09-10 2019-09-05 2019-09-07 2019-08-25 2019-09-05 2019-10-10 2019-10-15 ")` This function lubridate::int_overlaps() checks if two date intervals overlap or not by returning logical TRUE or FALSE. `int_overlaps

R: Determine if each date interval overlaps with all other date intervals in a dataframe

徘徊边缘 提交于 2020-08-25 03:21:32
问题 For each date interval row in my dataframe, I would like to determine whether it overlaps with all other date intervals or not. Excluding itself. A dataframe with start and end date, representing intervals: `data <- read.table(header=TRUE,text=" start.date end.date 2019-09-01 2019-09-10 2019-09-05 2019-09-07 2019-08-25 2019-09-05 2019-10-10 2019-10-15 ")` This function lubridate::int_overlaps() checks if two date intervals overlap or not by returning logical TRUE or FALSE. `int_overlaps

Splitting multiple date and time variables & computing time average in R

北城以北 提交于 2020-08-09 07:23:19
问题 I have the following dataset wherein, I have the person's ID, district and sub-district they live in along with the last date/time on which they uploaded data to the server. The variables "last_down_" contain the last date/time on which a person the uploaded data and are named in such a way that they show the date on which I had downloaded the data on the same. For example, "last_upload_2020-06-12" would mean I downloaded the data from the server on 12th June. For the below dataset, I would

Elegant way to get no of days to prev and next year using R?

北城以北 提交于 2020-07-30 03:29:34
问题 I have an R 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")) I would like to get the no of days to prev year and next year. I was trying something like the below library(lubridate) test_df$current_yr = year(mdy(test_df$date_1)) prev_yr = test_df$current_yr - 1 #(subtract 1 to get the prev year) next_yr = test_df$current_yr + 1 #(add 1 to get the prev year) days_to_prev_yr = days

Get the range and random days within that range using R

ぃ、小莉子 提交于 2020-07-23 06:20:23
问题 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

Get the range and random days within that range using R

僤鯓⒐⒋嵵緔 提交于 2020-07-23 06:19:19
问题 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