lubridate

add 1 business day to date in R

帅比萌擦擦* 提交于 2019-12-01 06:36:05
I have a Date object in R and would like to add 1 business day to this date. If the result is a holiday, I would like the date to be incremented to the next non-holiday date. Let's assume I mean NYSE holidays. How can I do this? Example: mydate = as.Date("2013-12-24") mydate + 1 #this is a holiday so I want this to roll over to the 26th instead I might use a combo of timeDate::nextBizDay() and roll=-Inf to set up a data.table lookup calendar, like this: library(data.table) library(timeDate) ## Set up a calendar for 2013 & 2014 cal <- data.table(date=seq(from=as.Date("2013-01-01"), by=1, length

add 1 business day to date in R

喜你入骨 提交于 2019-12-01 03:58:31
问题 I have a Date object in R and would like to add 1 business day to this date. If the result is a holiday, I would like the date to be incremented to the next non-holiday date. Let's assume I mean NYSE holidays. How can I do this? Example: mydate = as.Date("2013-12-24") mydate + 1 #this is a holiday so I want this to roll over to the 26th instead 回答1: I might use a combo of timeDate::nextBizDay() and roll=-Inf to set up a data.table lookup calendar, like this: library(data.table) library

Count the number of Fridays or Mondays in Month in R

一世执手 提交于 2019-12-01 03:48:47
I would like a function that counts the number of specific days per month.. i.e.. Nov '13 -> 5 fridays.. while Dec'13 would return 4 Fridays.. Is there an elegant function that would return this? library(lubridate) num_days <- function(date){ x <- as.Date(date) start = floor_date(x, "month") count = days_in_month(x) d = wday(start) sol = ifelse(d > 4, 5, 4) #estimate that is the first day of the month is after Thu or Fri then the week will have 5 Fridays sol } num_days("2013-08-01") num_days(today()) What would be a better way to do this? 1) Here d is the input, a Date class object, e.g. d <-

guess_formats + R + lubridate

江枫思渺然 提交于 2019-11-30 23:24:51
问题 I'm having trouble understanding how to use the guess_formats function in lubridate. I have a vector of dates in some unknown set/order of formats. I'd like to convert them to a Date object (or at least convert as many as possible). The following code is what I've tried: library(lubridate) sampleDates <- c("4/6/2004","4/6/2004","4/6/2004","4/7/2004", "4/6/2004","4/7/2004","2014-06-28","2014-06-30","2014-07-12", "2014-07-29","2014-07-29","2014-08-12") formats <- guess_formats(sampleDates, c(

Aggregation by time period in lubridate

蹲街弑〆低调 提交于 2019-11-30 20:06:28
This question asks about aggregation by time period in R, what pandas calls resampling. The most useful answer uses the XTS package to group by a given time period, applying some function such as sum() or mean(). One of the comments suggested there was something similar in lubridate, but didn't elaborate. Can someone provide an idiomatic example using lubridate? I've read through the lubridate vignette a couple times and can imagine some combination of lubridate and plyr, however I want to make sure there isn't an easier way that I'm missing. To make the example more real, let's say I want the

Converting chr “00:00:00” to date-time “00:00:00”

心已入冬 提交于 2019-11-30 17:38:18
问题 My question comes from this question. The question had the following character string. x <- "2007-02-01 00:00:00" y <- "02/01/2007 00:06:10" If you try to convert this string to date-class object, something funny happens. This is a sample from @nrusell's answer. as.POSIXct(x,tz=Sys.timezone()) [1] "2007-02-01 EST" as.POSIXct(y,format="%m/%d/%Y %H:%M:%S",tz=Sys.timezone()) [1] "2007-02-01 00:06:10 EST" As you see, 00:00:00 disappears from the first example. @Richard Scriven left the following

R aggregate a dataframe by hours from a date with time field

旧巷老猫 提交于 2019-11-30 15:44:00
I'm relatively new to R but I am very familiar with Excel and T-SQL. I have a simple dataset that has a date with time and a numeric value associated it. What I'd like to do is summarize the numeric values by-hour of the day. I've found a couple resources for working with time-types in R but I was hoping to find a solution similar to is offered excel (where I can call a function and pass-in my date/time data and have it return the hour of the day). Any suggestions would be appreciated - thanks! library(readr) library(dplyr) library(lubridate) df <- read_delim('DateTime|Value 3/14/2015 12:00:00

How to get a date from day of year

可紊 提交于 2019-11-30 14:48:49
I am working with a data set that looks a bit like this: Year Date Day_nr Value 1976 19-02-1976 50 167 1976 19-03-1976 79 140 1978 05-03-1978 64 200 1978 05-04-1978 95 200 1999 05-05-1999 125 89 1999 20-06-1999 171 79 I am then interested to estimate a polynomial model for each year depending on the day number as a x value. I then run a predict function to estimate the values of the model. I do that with the day numbers. My Data for the predict data frame then looks a bit like this just with more entries per year Year Day_nr Value 1976 53 167 1976 80 140 1978 69 300 1978 130 200 1999 140 89

R Time periods overlapping

孤者浪人 提交于 2019-11-30 14:45:33
With "lubridate" package in R, I can find out if two time periods overlapped. but Is there an efficient way to compute for how many days they overlapped. (for instance how many days a women smoked while pregnant. the pregnancy period and smoking period may overlap totally, partially or not at all) Here is an example with three women: preg_start<-as.Date(c("2011-01-01","2012-01-01","2013-01-01")) preg_end<-preg_start+270 # end after 9 months smoke_start<-as.Date(c("2011-02-01","2012-08-01","2014-01-01")) smoke_end<-smoke_start+100 # all three smoked 100 days data<-data.frame(cbind(preg_start

Extract week number from POSIXct object

家住魔仙堡 提交于 2019-11-30 09:12:36
Is there a function in lubridate to extract the week number? I've tried to search for that but couldn't find anything which serves the purpose. The week() function does something different. Description Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate, xts, > its, ti, jul, timeSeries, and fts objects. Weeks is the number of complete seven day periods that have occured between the date and January 1st, plus one. isoweek returns the week as it would appear in the ISO 8601 system, which uses a reoccuring leap week. Use strftime : dateRange <- c("2008-10-01