x <- function( s ) {
return(
tryCatch({
r = eval( parse( text=s ) )
if( is.atomic( r ) ) {
r
}
else {
s
}
},
warning = function( w ) {
s
},
error = function( e ) {
s
})
)
}
when <- function( d, n = 0, format = "%Y-%m-%d" ) {
as.Date( d, format = format ) + x( n )
}
annal <- function( days = 0, format = "%Y-%m-%d", oformat = "%B %d, %Y" ) {
format( when( anchor, days ), format = oformat )
}
year <- function( days = 0, format = "%Y-%m-%d" ) {
annal( days, format, "%Y" )
}
weekday <- function( n ) {
weekdays( when( anchor, n ) )
}
concat <- paste0
cms <- function( n, ordinal = FALSE ) {
n <- x( n )
if( n == 0 ) {
if( ordinal ) {
return( "zeroth" )
}
return( "zero" )
}
if( n < 0 ) {
result = "negative "
n = abs( n )
}
if( n > 100 ) {
return( format( n, big.mark=",", trim=TRUE, scientific=FALSE ) )
}
if( n == 100 ) {
if( ordinal ) {
return( "one hundredth" )
}
return( "one hundred" )
}
small = c(
"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"
)
ord_small = c(
"first", "second", "third", "fourth", "fifth",
"sixth", "seventh", "eighth", "ninth", "tenth",
"eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth",
"sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth"
)
if( n < 20 ) {
if( ordinal ) {
return( .subset( ord_small, n %% 100 ) )
}
return( .subset( small, n %% 100 ) )
}
tens = c( "",
"twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"
)
ord_tens = c( "",
"twentieth", "thirtieth", "fortieth", "fiftieth",
"sixtieth", "seventieth", "eightieth", "ninetieth"
)
ones_index = n %% 10
n = n %/% 10
if( ones_index == 0 ) {
if( ordinal ) {
return( .subset( ord_tens, n ) )
}
return( .subset( tens, n ) )
}
if( ordinal ) {
unit_1 = .subset( ord_small, ones_index )
}
else {
unit_1 = .subset( small, ones_index )
}
unit_10 = .subset( tens, n )
concat( unit_10, concat( "-", unit_1 ) )
}
elapsed <- function( began, ended, s = "same day" ) {
began = when( anchor, began )
ended = when( anchor, ended )
if( as.integer( ended - began ) < 0 ) {
tempd = began
began = ended
ended = tempd
}
years = length( seq( from = began, to = ended, by = 'year' ) ) - 1
if( years > 0 ) {
began = seq( began, length = 2, by = concat( years, " years" ) )[2]
years = pl.numeric( "year", years )
}
else {
years = ""
}
months = length( seq( from = began, to = ended, by = 'month' ) ) - 1
if( months > 0 ) {
began = seq( began, length = 2, by = concat( months, " months" ) )[2]
months = pl.numeric( "month", months )
}
else {
months = ""
}
days = length( seq( from = began, to = ended, by = 'day' ) ) - 1
if( days > 0 ) {
days = pl.numeric( "day", days )
}
else {
days = ""
}
if( years <= 0 && months <= 0 && days <= 0 ) {
return( s )
}
s <- c( years, months, days )
s <- s[ s != "" ]
r <- paste( s, collapse = ", " )
if( length( s ) > 2 ) {
return( gsub( "(.*),", "\\1, and", r ) )
}
gsub( "(.*),", "\\1 and", r )
}
pl.numeric <- function( s, n ) {
concat( cms( n ), concat( " ", pluralize( s, n ) ) )
}
season <- function( n, format = "%Y-%m-%d" ) {
WS <- as.Date("2016-12-15", "%Y-%m-%d")
SE <- as.Date("2016-03-15", "%Y-%m-%d")
SS <- as.Date("2016-06-15", "%Y-%m-%d")
AE <- as.Date("2016-09-15", "%Y-%m-%d")
d <- when( anchor, n )
d <- as.Date( strftime( d, format="2016-%m-%d" ) )
ifelse( d >= WS | d < SE, "Winter",
ifelse( d >= SE & d < SS, "Spring",
ifelse( d >= SS & d < AE, "Summer", "Autumn" )
)
)
}
lc <- function( s ) {
concat( tolower( substr( s, 1, 1 ) ), substr( s, 2, nchar( s ) ) )
}
uc <- function( s ) {
concat( toupper( substr( s, 1, 1 ) ), substr( s, 2, nchar( s ) ) )
}
days <- function( d1, d2, format = "%Y-%m-%d" ) {
dates = c( d1, d2 )
dt = strptime( dates, format = format )
as.integer( difftime( dates[2], dates[1], units = "days" ) )
}
years <- function( began, ended ) {
began = when( anchor, began )
ended = when( anchor, ended )
if( as.integer( ended - began ) < 0 ) {
tempd = began
began = ended
ended = tempd
}
length( seq( from = began, to = ended, by = 'year' ) ) - 1
}
month <- function( n ) {
.subset( month.name, x( n ) )
}
money <- function( n ) {
formatC( x( n ), format="d" )
}