tidyverse / lubridate Goto Github PK
View Code? Open in Web Editor NEWMake working with dates in R just that little bit easier
Home Page: https://lubridate.tidyverse.org
License: GNU General Public License v3.0
Make working with dates in R just that little bit easier
Home Page: https://lubridate.tidyverse.org
License: GNU General Public License v3.0
Once you have run roxygenise, you can use the following code to run all of the examples in the documentation. You might find the following function helpful - it's a bit faster than using R CMD check because it just runs the examples:
library(plyr)
library(tools)
run_examples <- function(path) {
files <- dir(path, full = TRUE)
parsed <- llply(files, parse_Rd)
names(parsed) <- basename(files)
extract_examples <- function(rd) {
tags <- tools:::RdTags(rd)
paste(unlist(rd[tags == "\\examples"]), collapse = "")
}
examples <- llply(parsed, extract_examples)
m_ply(cbind(file = names(examples), example = examples),
function(file, example) {
cat("Checking ", file, "...\n", sep = "")
eval(parse(text = example))
}
)
}
Instead of storing durations as data.frames, store them as atomic numbers where the 9 rightmost digits are seconds and the remainder are months.
Blocks of 126239400 seconds can be converted to four years, since this is the number of seconds in three years and a leap year. This will prevent the number of seconds from spilling into the months columns. (An exception will have to be made for years like 2000 which should be leap years but are not. I'm still thinking about this.)
This change will require:
the durations format automatically prevents seconds data from spilling into months data by moving four year blocks of seconds to the months side. This works because 3 years + 1 leap year is (normally) a fixed number of seconds.
Sometimes, however, a four year block does not have a leap year (for example 2000). I nthis case the duration will be 1 day too long.
I need to create a way to test for this and fix it.
Did we still want to make a function that would allow a user to expand or contract a period for weekends? We mentioned it at the beginning of the project. But I'm not sure what that would specifically look like. Perhaps:
today + business_days(days(3))
today + business_hours(hours(50))
tomorrow + market_days(days(2))
?
maybe business_days() could modify the class of its argument and then we could write a +/- method for that new class
something for version 2?
It looks like it's disappeared in the latest version, so please go back in time and figure out what happened to it!
Remember to self: consider BC dates and time (Before Christ/ Before Common Era)
as.period.interval(interval, periods)
"bites out" as many periods as possible of the largest units listed in periods
repeats for second largest units using the remaining interval
until all listed units have been handled
appends remaining number of seconds to the period
Into file for each field type: accessors-yday.r
, accessors-mday.r
etc.
recreate the table in RNews 4/1 with lubridate. Everything should appear less complicated
durations of zero months and zero seconds don't display as anything
e.g.
d - d
Create code to make the help files using roxygenize() save in a
folder called man/
for example:
y - d = 1 year, -1 weeks and 6 days
but it would be better to keep everything positive
(i.e, y - d = 11 months and ? days
= ?days (364 or 365))
On second thought, is the current method preferred?
(what about y - d = 1 year and -1 days)
decide how to handle different orders of months and years. remove commenting
Add a note in the ?update.Date() explaining that it is just a setting
function (not an adding function) . Include examples demonstrating
this
e.g. update(ymd(20080131), month = 2)
and then delete.
You only need to use @method
if you're documenting a particular method. Otherwise just use @alias
currently:
a
1 month and 1 secondc(a,a)
$months
[1] 1
$seconds
[1] 1
$months
[1] 1
$seconds
[1] 1
list(a,a)
[[1]]
1 month and 1 second
[[2]]
1 month and 1 second
data.frame( one = a)
one.months one.seconds
1 1 1
any suggestions?
lubridate calculates the difference between dates in seconds
a <- now() # "2009-07-27 11:01:12 CDT"
z <- now() #"2009-07-27 11:01:19 CDT"
z - a = 6.10436415672302 seconds
This difference is automatically stored as a duration. Since durations cannot handle intervals greater than 5e+08 seconds, we cannot subtract dates more than ~16 years apart.
a <- a + 250000001 # "2017-06-28 23:27:53 CDT"
z <- z - 250000011 # "2001-08-24 22:34:28 CDT"
z - a = Error in new_duration(second = as.numeric(x, units = "secs")) : seconds overflow: see 'duration' documentation
This limits the date combinations that we can subtract, but also hampers pretty.date() for date ranges greater than 16 years.
A user may have a list of dates in POSIXct format, but without the class attribute, i.e. they'll just look like a list of numbers.
He or she could easily change them to POSIXct format using
as.POSIXct(x, origin = "1970-01-01")
Or whatever the origin date is (should they originate from another point).
We haven't written any commands that would handle these dates. Do we want to add to our existing commands? Or do we expect the user to get his or her POSIXct dates in shape with the above code before using lubridate?
It would be nice to have options for wday and month to return an ordered factor of strings instead of raw numbers.
Need method like pretty() from base R that given a date range, provides a set of pretty breaks that nicely span the range.
year<-(x, y) and month<-(x,y) both fail if x is a POSIXlt object with an unspecified timezone.
Links should be written like \code{\link{a}}, \code{\link{b}}
Including those in packages xts, its, ti, chron
With base::difftime() R uses "secs", "mins", "days", and "weeks" for unit inputs.
With update.Date and other functions we're using "second", "minute", "hour", "*day", "week", "month" "year".
Should we keep our unit names consistent with base::R?
with the updated format, negative durations print horribly
new_duration(secs = -1, mins = 1, hours = -1) [1] "-4 years, 208 weeks, 4 days, 23 hours and 59 seconds"
The code for '*.duration' and '/.duration' uses as.data.frame.duration() which has been disabled. When we decide how to handle data frames, decide whether we want to keep this code.
...to use while documenting.
So far I can emulate this for anything
\#' TITLE \#' Sub Title \#' (blank line) \#' Paragraph explaining stuff \#' (blank line) \#' @param \#' @author \#' @keywords \#' examples \#' example1() \#' example2() \#example <- function()
Still to learn:
how do I combine documentation for functions?
add a see also tag?
put more than one author?
add a usage tag?
how do I link to other documentation pages?
Should list all time/date packages that lubridate works with
We should add a message to ymd( ) that appears depending on where ymd(
) is being used. The message reveals what format is being used.
ymd(..., advice = should_advise(...))
Should now always be the time the package is loaded (as its currently written)? It will never really be "now" when its called
now <- Sys.time()
Or should it be a function that returns the "now" of whenever its called?
now <- function() Sys.time()
Test the code and save in a folder called tests/
could fix on last line by changing object to object[1], but I'm not sure why this result is happening.
Maybe these should go in accessors-conversion.r
?
for example update(minute = 1, days = c(1:5)) should work
The guess_formats() code uses "mlply" from the plyr package.
Is it possible to make lubridate also load plyr when it installs?
Or could we include the code for mlply in the lubridate package?
...because it uses ISOdate() which returns and NA when the month value is not an integer. Which means we won't be able to add partial months, using add_duration_to_date.
I wrote an alternate version of "month<-" that uses the sequence approach, and put it in the code after the original version.
This code works, but users will have to scratch their head twice instead of once to understand the 'month<-' function (because it will have the sequence script).
What do you suggest?
Should write guess_format() on
ymd() to choose between %y and %Y. Add "%" to
the end of each string and then look for format strings that also end
in "%". i.e, "__ __ __ %" Otherwise both will work and %y will just
throw out the last extra two numbers
lubridate should override difftime to ensure that it always returns difference in seconds.
Months and years are periods of non-fixed duration. Does lubridate add partial periods or require users to specify a partial period as a duration in days?
ex.
2009-06-14 + months(2.5)
or
2009-06-14 + months(2) + days(15)
...just in general.
But its also bad for lubridate. When DST changes, days are not specifically 24 hours (They may be 23 or 25 hours).
date <- as.POSIXlt("2009-03-07 00:00:00") #"2009-03-07 00:00:00 CST"
If you work with financial markets, you may want
date + days(2) = "2009-03-09 00:00:00 CDT"
If you work in physical sciences, you may want
date + days(2) = "2009-03-09 01:00:00 CDT"
I've written code for both ways. But we can't ask people what they want each time they use the + function. Is there a way to make an option the user can set like:
options(days = "exact")
options(days = "relative")
?
This error message crashes R whenever I set options(error = recover), which is interfering with debugging
Whenever I type an open parenthesis immediately following a word, the GUI console immediately displays
Error in try(gsub("\s+", " ", paste(capture.output(print(args(options))), :
unused argument(s) (silent = TRUE)
without even waiting for me to execute the command.
Since a duration is also a data.frame, putting it into data structures that do not retain its class attributes converts it back into a data frame without warning.
we can prevent this or insert warning by writing a c.duration() but this will not be called if the first element in the vector is not a duration.
Which of these would we like to users to use with durations, and what do we want the output ot look like?
convert a date to a year + decimal (e.g. 2000-07-01 ~ 2000.4953)
repopulate parse.r with the helper functions dmy() mdy() myd() etc. etc.
Add code to +, -, *, /, so that if n1 < n2, our functions can expand it like a data frame and then perform the operation.
"+.duration" <- "+.difftime" <- function() does not work because difftimes are added according to Ops.difftime()
"+.duration" <- "Ops.difftime()" doesn't work because Ops.difftime() needs a value for ".Generic"
E.g. hour<-
should be an alias of hour
What do you think of the print.duration output? Here's the current output:
Duration: 0 years, 0 months, 0 weeks, 0 days, 0 hours, 0 minutes and 1 seconds
Should I make it exclude the zero entries and discriminate between 1 and more than one? (day vs. days)? Would it be a bad idea to get rid of "Duration:" ?
1 second
How can lubridate support partial dates? (e.g. year + month, or year + day). Should setting (e.g.) yday to NA create a partial date? What sort of data structure is needed to support these dates?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.