lubridate

Using the result of summarise (dplyr) to mutate the original dataframe

房东的猫 提交于 2019-11-29 08:08:41
I have a rather big dataframe with a column of POSIXct datetimes (~10yr of hourly data). I would flag all the rows in which the day falls in a Daylight saving period. For example if the Daylight shift starts on '2000-04-02 03:00:00' (DOY=93) i would like that the two previous hours of DOY=93 could be flagged. Although I am a newbie of dplyr I would use this package as much as possible and avoid for-loops as much as possible For example: library(lubridate) sd = ymd('2000-01-01',tz="America/Denver") ed = ymd('2005-12-31',tz="America/Denver") span = data.frame(date=seq(from=sd,to=ed, by="hour"))

Have lubridate subtraction return only a numeric value

倖福魔咒の 提交于 2019-11-29 05:44:33
I have one variable called Started which is the date on which human subjects enrolled in a study and another variable called dos1 which is the date upon which the subject last had surgery. I want to work out how many months since their last surgery to the day of enrollment. I tried: as.period(syrrupan$Started-syrrupan$dos1,units=c("month")) I expected this to give me something like: 14, 18, 1, 26 With each number being the number of months. Instead I get: 1 year, -4 months, -5 days and -1 hours 1 year, -5 months, -23 days and -1 hours 1 year, -7 months, 2 days and -1 hours 1 year, -8 months,

Is there a fast parser for date

一世执手 提交于 2019-11-29 04:48:36
For datetimes fasttime provides very fast parsing to POSIXct library('fasttime') library('lubridate') library('microbenchmark') # parse character to POSIXct Sys.setenv(TZ='UTC') test <- rep('2011-04-02 11:01:00',1e4) microbenchmark( test1 <- fastPOSIXct(test), test2 <- fast_strptime(test,format='%Y-%m-%d %H:%M:%S'), test3 <- as.POSIXct(test, format='%Y-%m-%d %H:%M:%S'), test4 <- ymd_hms(test), times=100) Unit: microseconds expr min lq mean median uq max test1 <- fastPOSIXct(test) 663.123 692.337 1409.448 701.821 712.4965 71231.585 test2 <- fast_strptime(test, format = "%Y-%m-%d %H:%M:%S") 1026

dplyr: grouping and summarizing/mutating data with rolling time windows

大城市里の小女人 提交于 2019-11-29 04:32:58
I have irregular timeseries data representing a certain type of transaction for users. Each line of data is timestamped and represents a transaction at that time. By the irregular nature of the data some users might have 100 rows in a day and other users might have 0 or 1 transaction in a day. The data might look something like this: data.frame( id = c(1, 1, 1, 1, 1, 2, 2, 3, 4), date = c("2015-01-01", "2015-01-01", "2015-01-05", "2015-01-25", "2015-02-15", "2015-05-05", "2015-01-01", "2015-08-01", "2015-01-01"), n_widgets = c(1,2,3,4,4,5,2,4,5) ) id date n_widgets 1 1 2015-01-01 1 2 1 2015-01

Time difference in years with lubridate?

喜你入骨 提交于 2019-11-29 01:53:25
I would like to use lubridate to calculate age in years given their date of birth and today's date. Right now I have this: library(lubridate) today<-mdy(08312015) dob<-mdy(09071982) today-dob which gives me their age in days. Paul Hiemstra This is the lubridate approach I would take: interval(dob, today) / years(1) Yields the answer of 32 years. Note that the function will complain that it cannot express the remainder of the fraction of the year. This is because year is not a fixed concept, i.e. 366 in leap years and 365 in non-leap years. You can get an answer with more detail in regard to

Why are lubridate functions so slow when compared with as.POSIXct?

我是研究僧i 提交于 2019-11-28 23:03:51
As the title goes. Why is the lubridate function so much slower? library(lubridate) library(microbenchmark) Dates <- sample(c(dates = format(seq(ISOdate(2010,1,1), by='day', length=365), format='%d-%m-%Y')), 50000, replace = TRUE) microbenchmark(as.POSIXct(Dates, format = "%d-%b-%Y %H:%M:%S", tz = "GMT"), times = 100) microbenchmark(dmy(Dates, tz ="GMT"), times = 100) Unit: milliseconds expr min lq median uq max 1 as.POSIXct(Dates, format = "%d-%b-%Y %H:%M:%S", tz = "GMT") 103.1902 104.3247 108.675 109.2632 149.871 2 dmy(Dates, tz = "GMT") 184.4871 194.1504 197.8422 214.3771 268.4911 Tyler

R sequence of dates with lubridate

不羁的心 提交于 2019-11-28 22:16:58
Hi I'm trying to get a sequence of dates with lubridate This doesn't work seq(ymd('2012-04-07'),ymd('2013-03-22'),by=week(1)) the base command seq(as.Date('2012-04-7'),as.Date('2013-03-22'),'weeks') does, but I'd like to know if there is an elegant way to do this with lubridate. EDIT Please ignore : solved myself so leaving up for posterity only. Happy to have this deleted if necessary. seq(ymd('2012-04-07'),ymd('2013-03-22'),by='weeks') Does the trick ymd is a wrapper to parse date strings and returns a POSIXct object. You simply need to use standard terminology described in ?seq.POSIXt (not

How to extract Month from date in R

故事扮演 提交于 2019-11-28 21:15:45
I am using the lubridate package and applying the month function to extract month from date. I ran the str command on date field and I got Factor w/ 9498 levels "01/01/1979","01/01/1980",..: 5305 1 1 1 1 1 1 1 1 1 ... > v1$Date<-month(v1$Date) Error in as.POSIXlt.character(as.character(x), ...) : character string is not in a standard unambiguous format Here is an example of my data frame https://drive.google.com/file/d/0B6cqWmwsEk20Q2dHblhXZi14Wk0/edit?usp=sharing I don't know what I am doing wrong. ?month states: Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo

Using lubridate and ggplot2 effectively for date axis

有些话、适合烂在心里 提交于 2019-11-28 13:39:10
Consider this example: library(ggplot2) library(lubridate) set.seed(4) date <- seq(from = as.POSIXct("2012-01-01"), to = as.POSIXct("2014-12-31"), by = "days") value <- c(rnorm(274, 50, 1), rnorm(274, 55, 1), rnorm(274, 55, 2), rnorm(274, 60, 2)) df <- data.frame(date, value) head(df) # date value # 1 2012-01-01 50.21675 # 2 2012-01-02 49.45751 # 3 2012-01-03 50.89114 # 4 2012-01-04 50.59598 # 5 2012-01-05 51.63562 # 6 2012-01-06 50.68928 ggplot(df, aes(x=yday(date), y=value, color=factor(year(date)))) + geom_line() Which generates this plot: What are some ways to make the axis formatted as a

Subtracting months - issue with last day of month?

北城余情 提交于 2019-11-28 11:18:20
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 this isn't working, and/or suggest a more "robust" way around it. Does it have something to do with today