lubridate

How to round floor_date() to arbitrary date?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-16 12:22:58
问题 I am using floor_date to round dates to the weeks: library(lubridate) floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days') [1] "2016-08-22 UTC" However, I would like to set a specific date as the beginning of the first week. For instance, I would like to set "2016-08-26 UTC" as first week. As a workaround I was trying to tweak getOption("lubridate.week.start") , but I cannot get to change the starting weekday regardless of what I pass: > floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days',

How to round floor_date() to arbitrary date?

时光怂恿深爱的人放手 提交于 2020-01-16 12:22:17
问题 I am using floor_date to round dates to the weeks: library(lubridate) floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days') [1] "2016-08-22 UTC" However, I would like to set a specific date as the beginning of the first week. For instance, I would like to set "2016-08-26 UTC" as first week. As a workaround I was trying to tweak getOption("lubridate.week.start") , but I cannot get to change the starting weekday regardless of what I pass: > floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days',

How to round floor_date() to arbitrary date?

可紊 提交于 2020-01-16 12:21:47
问题 I am using floor_date to round dates to the weeks: library(lubridate) floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days') [1] "2016-08-22 UTC" However, I would like to set a specific date as the beginning of the first week. For instance, I would like to set "2016-08-26 UTC" as first week. As a workaround I was trying to tweak getOption("lubridate.week.start") , but I cannot get to change the starting weekday regardless of what I pass: > floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days',

find last day of month in a sequence of dates

淺唱寂寞╮ 提交于 2020-01-15 07:13:47
问题 I have a sequence of dates like this ds <- seq(as.Date("2011-02-01"), length=100, by="day") I want to find the indices of the last days of each month I can do it like this last_day <- seq(as.Date("2011-02-01"), length=10, by="1 month") - 1 which(ds %in% last_day) my problem is that my sequence of dates is not complete, some dates are missing and sometimes this can be the last day. For example, I removed the last day of February ds[ds == as.Date('2011-02-28')] <- NA The new last day should now

find last day of month in a sequence of dates

拈花ヽ惹草 提交于 2020-01-15 07:13:11
问题 I have a sequence of dates like this ds <- seq(as.Date("2011-02-01"), length=100, by="day") I want to find the indices of the last days of each month I can do it like this last_day <- seq(as.Date("2011-02-01"), length=10, by="1 month") - 1 which(ds %in% last_day) my problem is that my sequence of dates is not complete, some dates are missing and sometimes this can be the last day. For example, I removed the last day of February ds[ds == as.Date('2011-02-28')] <- NA The new last day should now

“%m %” operator with mapply

一个人想着一个人 提交于 2020-01-14 13:42:28
问题 I am trying to use mapply to add months to the current dates in columns a and b of my dataframe. Here is the code to create a sample data frame: library(lubridate) a <- as.Date(c("2012-01-11","2012-06-30","2012-04-18")) b <- as.Date(c("2013-04-21","2012-03-22","2012-05-01")) df <- data.frame(a,b) I can use mapply("+",df, c(30,30)) to add 30 days to both columns of dates. However, when I try to use the command mapply("%m+%",df, months(1:2)) I get the error message: Error in .setupMethodsTables

“%m %” operator with mapply

☆樱花仙子☆ 提交于 2020-01-14 13:40:18
问题 I am trying to use mapply to add months to the current dates in columns a and b of my dataframe. Here is the code to create a sample data frame: library(lubridate) a <- as.Date(c("2012-01-11","2012-06-30","2012-04-18")) b <- as.Date(c("2013-04-21","2012-03-22","2012-05-01")) df <- data.frame(a,b) I can use mapply("+",df, c(30,30)) to add 30 days to both columns of dates. However, when I try to use the command mapply("%m+%",df, months(1:2)) I get the error message: Error in .setupMethodsTables

Calculating age using mutate with lubridate functions [duplicate]

半城伤御伤魂 提交于 2020-01-14 12:56:04
问题 This question already has answers here : Calculate ages in R (8 answers) Closed 2 years ago . I would like to calculate age based on birth date. If I use lubridate, I would just run the following as in Efficient and accurate age calculation (in years, months, or weeks) in R given birth date and an arbitrary date as.period(new_interval(start = birthdate, end = givendate))$year However, when I tried to use mutate in dplyr to create the new variable, I ran into an error. library(dplyr); library

Manage multiple Date formats to POSIXct [duplicate]

為{幸葍}努か 提交于 2020-01-11 13:28:11
问题 This question already has answers here : Convert variable with mixed date formats to one format (3 answers) Closed 2 years ago . I have two dataframe with two different format of date the first is "%d/%m/%Y %H:%M:%S" and the second "%Y-%m-%d %H:%M:%S". I want to create a function that convert to POSIXct by indicating the format. My code: date_func <- function(df){ colnum <- grep("DATE", colnames(df)) df[, (colnum) := lapply(.SD, dmy_hms), .SDcols = colnum] return(df) } For the first format it

First day of the month from a POSIXct date time using lubridate

℡╲_俬逩灬. 提交于 2020-01-10 02:29:23
问题 Given a POSIXct date time, how do you extract the first day of the month for aggregation? library(lubridate) full.date <- ymd_hms("2013-01-01 00:00:21") 回答1: lubridate has a function called floor_date which rounds date-times down. Calling it with unit = "month" does exactly what you want: library(lubridate) full.date <- ymd_hms("2013-01-01 00:00:21") floor_date(full.date, "month") [1] "2013-01-01 UTC" 回答2: I don't see a reason to use lubridate: full.date <- as.POSIXct("2013-01-11 00:00:21",