lubridate

Subtracting months - issue with last day of month?

断了今生、忘了曾经 提交于 2019-11-27 06:10:29
问题 Quick question on dates in R. Check out this snippet of code: Sys.Date() - months(3) # [1] "2013-12-31" Sys.Date() - months(18) # [1] NA I've loaded the package lubridate and followed the instructions provided , and I can't quite get my head around this behavior. It used to work just fine, today's the first day that I've noticed that a subtraction of more than 12 months from today's date yields NA (subtracting less than 12 months works fine). I'd appreciate if anyone can explain to me why

Summing rows by month in R

非 Y 不嫁゛ 提交于 2019-11-27 05:52:31
问题 So I have a data frame that has a date column, an hour column and a series of other numerical columns. Each row in the data frame is 1 hour of 1 day for an entire year. The data frame looks like this: Date Hour Melbourne Southern Flagstaff 1 2009-05-01 0 0 5 17 2 2009-05-01 2 0 2 1 3 2009-05-01 1 0 11 0 4 2009-05-01 3 0 3 8 5 2009-05-01 4 0 1 0 6 2009-05-01 5 0 49 79 7 2009-05-01 6 0 425 610 The hours are out of order because this is subsetted from another data frame. I would like to sum the

Adding 15 business days in lubridate

╄→尐↘猪︶ㄣ 提交于 2019-11-27 03:34:15
问题 I have a long list of start dates of a certain procedure. Rules require the procedure to be completed in, at most, 6 business days. I wish to compute the deadline. Using lubridate in R, I can get a six-day deadline thus > library(lubridate) > date.in <- dmy(c("30-8-2001", "12-1-2003", "28-2-2003", "20-5-2004")) > date.in [1] "2001-08-30 UTC" "2003-01-12 UTC" "2003-02-28 UTC" "2004-05-20 UTC" > deadline.using.days <- date.in + days(6) > deadline.using.days [1] "2001-09-05 UTC" "2003-01-18 UTC"

row not consolidating duplicates in R when using multiple months in Date Filter

旧街凉风 提交于 2019-11-26 23:37:11
问题 I am using the following code to summarize my data by a column library(data.table, warn.conflicts = FALSE) library(lubridate, warn.conflicts = FALSE) ################ ## PARAMETERS ## ################ # Set path of major source folder for raw transaction data in_directory <- "C:/Users/NAME/Documents/Raw Data/" # List names of sub-folders (currently grouped by first two characters of CUST_ID) in_subfolders <- list("AA-CA", "CB-HZ") # Set location for output out_directory <- "C:/Users/NAME

Plot dates on the x axis and time on the y axis with ggplot2

 ̄綄美尐妖づ 提交于 2019-11-26 23:15:47
问题 I have read in a series of 37 dates and times that an event happened. It is now sitting as a POSIXlt object. I want a graphic representation of the times that the events happened on each day. So the x axis should be the date and y axis should be the time of day. then I tried to plot it with ggplot2 qplot(day(dttm), hour(dttm)) That is kind of what I want but it does not have the resolution of minutes. How do I have hours and minutes included in the y axis? Here is some sample data dttm [1]

Convert dd/mm/yy and dd/mm/yyyy to Dates

旧街凉风 提交于 2019-11-26 23:11:56
I have some a character vector with dates in various formats like this dates <- c("23/11/12", "20/10/2012", "22/10/2012" ,"23/11/12") I want to convert these to Dates. I have tried the very good dmy from the lubridate package, but this does not work: dmy(dates) [1] "0012-11-23 UTC" "2012-10-20 UTC" "2012-10-22 UTC" "0012-11-23 UTC" It is treating the /12 year as if it is 0012. So I now am trying regular expression to select each type and individually convert to dates using as.Date(). However the regular expression I have tried to select the dd/mm/yy only does not work. dates[grep('[0-9]{2}/[0

Check if a date is within an interval in R

谁都会走 提交于 2019-11-26 17:06:22
问题 I have these three intervals defined: YEAR_1 <- interval(ymd('2002-09-01'), ymd('2003-08-31')) YEAR_2 <- interval(ymd('2003-09-01'), ymd('2004-08-31')) YEAR_3 <- interval(ymd('2004-09-01'), ymd('2005-08-31')) (in real life, I have 50 of these) I have a dataframe (called df ) with a column full of lubridate formatted dates. I'd like to append a new column on df which has the appropriate value YEAR_n , depending on which interval the date falls within. Something like: df$YR <- ifelse(df$DATE

Calculating Time Difference between two columns

为君一笑 提交于 2019-11-26 16:55:28
问题 After converting factors in POSIXCT format and then applying datetime format, I want to take the difference of datetime between 2 pos1 and pos2. However, when I do that for a specific item I get the right answer in the console but when I do the operation on the whole set the console outputs just number and also the dateframe reflects those number as you can see. How can I get the hours in the dataframe when I am trying to take the difference? I am using lubridate package, is there any

Convert character to Date in R

▼魔方 西西 提交于 2019-11-26 16:34:24
I am relatively new to R, but this is the first time I have had to deal with date conversions. I read in my data from a CSV (using read.table()), but I shorted the data to highlight my issue. When read into R, the Date field is character. Simply, most of my dates get coerced correctly, except for a few instances. The example below will hopefully show you what is going on. # my attempt to coerce the date -- uses the stringr package prods.all$Date2 <- as.Date(str_sub(prods.all$Date, 1, str_locate(prods.all$Date, " ")[1]-1), "%m/%d/%Y") # grab two rows to highlight my issue temp <- prods.all[c

How to flatten / merge overlapping time periods

☆樱花仙子☆ 提交于 2019-11-26 16:30:36
I have a large data set of time periods, defined by a 'start' and and an 'end' column. Some of the periods overlap. I would like to combine (flatten / merge / collapse) all overlapping time periods to have one 'start' value and one 'end' value. Some example data: ID start end 1 A 2013-01-01 2013-01-05 2 A 2013-01-01 2013-01-05 3 A 2013-01-02 2013-01-03 4 A 2013-01-04 2013-01-06 5 A 2013-01-07 2013-01-09 6 A 2013-01-08 2013-01-11 7 A 2013-01-12 2013-01-15 Desired result: ID start end 1 A 2013-01-01 2013-01-06 2 A 2013-01-07 2013-01-11 3 A 2013-01-12 2013-01-15 What I have tried: require(dplyr)