lubridate

Using lubridate and ggplot2 effectively for date axis

删除回忆录丶 提交于 2019-11-27 19:30:24
问题 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

Is there a fast parser for date

你。 提交于 2019-11-27 18:30:40
问题 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

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

时间秒杀一切 提交于 2019-11-27 18:19:27
问题 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

Time difference in years with lubridate?

巧了我就是萌 提交于 2019-11-27 15:09:39
问题 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. 回答1: 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

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

梦想的初衷 提交于 2019-11-27 14:31:32
问题 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

Add/subtract 6 months (bond time) in R using lubridate

大城市里の小女人 提交于 2019-11-27 12:26:19
I am looking to add and subtract six months reliably with lubridate . For example, adding six months to 12/31/2014 should result in 6/30/2015 , and adding to 2/28/2014 should result in 8/31/2014 The issue with as.Date("2014-12-31") + months(6) , is that it yields an NA . Alternatively, the second result is 8/28/2014 because it doesn't just add 6 months to the month and then know where the day should end up dependent upon the month. Is there any way to quickly correct this? At the moment, I am building a function to basically use a switch and consider each month, but this is very long and I am

Create a Vector of All Days Between Two Dates

走远了吗. 提交于 2019-11-27 11:38:50
Is there an easy way in R for me to itemize all valid days that occurred between two specified dates? For instance, I'd like the following inputs: itemizeDates(startDate="12-30-11", endDate="1-4-12") To produce the following dates: "12-30-11" "12-31-11", "1-1-12", "1-2-12", "1-3-12", "1-4-12" I'm flexible on classes and formatting of the dates, I just need an implementation of the concept. You're looking for seq > seq(as.Date("2011-12-30"), as.Date("2012-01-04"), by="days") [1] "2011-12-30" "2011-12-31" "2012-01-01" "2012-01-02" "2012-01-03" [6] "2012-01-04" Or, you can use : > as.Date(as.Date

extract hours and seconds from POSIXct for plotting purposes in R

*爱你&永不变心* 提交于 2019-11-27 11:11:33
Suppose I have the following data.frame foo start.time duration 1 2012-02-06 15:47:00 1 2 2012-02-06 15:02:00 2 3 2012-02-22 10:08:00 3 4 2012-02-22 09:32:00 4 5 2012-03-21 13:47:00 5 And class(foo$start.time) returns [1] "POSIXct" "POSIXt" I'd like to create a plot of foo$duration v. foo$start.time . In my scenario, I'm only interested in the time of day rather than the actual day of the year. How does one go about extracting the time of day as hours:seconds from POSIXct class of vector? This is a good question, and highlights some of the difficulty in dealing with dates in R. The lubridate

Is there a more elegant way to convert two-digit years to four-digit years with lubridate?

China☆狼群 提交于 2019-11-27 08:49:57
If a date vector has two-digit years, mdy() turns years between 00 and 68 into 21st Century years and years between 69 and 99 into 20th Century years. For example: library(lubridate) mdy(c("1/2/54","1/2/68","1/2/69","1/2/99","1/2/04")) gives the following output: Multiple format matches with 5 successes: %m/%d/%y, %m/%d/%Y. Using date format %m/%d/%y. [1] "2054-01-02 UTC" "2068-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC" "2004-01-02 UTC" I can fix this after the fact by subtracting 100 from the incorrect dates to turn 2054 and 2068 into 1954 and 1968. But is there a more elegant and less

Assigning Dates to Fiscal Year

痞子三分冷 提交于 2019-11-27 08:46:07
I'm trying to come up with some code that will look at a date and then assign it to a fiscal year. I'm totally stuck. I have a variable that contains dates in POSIXct format: df$Date #2015-05-01 CST #2015-04-30 CST #2014-09-01 CST What I need to be able to do is take those dates and return a fiscal year, which runs from May 1 - April 30. For example, Fiscal Year 2016 runs 2015-05-01 through 2016-04-30. Results would look something like this: df$Date df$FiscalYear #2015-05-01 CST #FY2016 #2015-04-30 CST #FY2015 #2014-09-01 CST #FY2015 Is there any easy way to do this? Here are some alternatives