Skip to content

Commit

Permalink
0.99.53.1
Browse files Browse the repository at this point in the history
  • Loading branch information
yourpresidentuniversal committed Feb 2, 2024
1 parent 1486256 commit ef135a3
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 111 deletions.
14 changes: 12 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ useDynLib(DescTools, .registration=TRUE)

export(

"%c%","%like%","%like any%", "%nin%","%overlaps%","AddMonths","AddMonthsYM","Agree","AllDuplicated","aovlDetails","aovlErrorTerms","as.matrix.xtabs","AscToChar","Assocs","Atkinson","AUC","AxisBreak","BartelsRankTest","BinomCI","BinomDiffCI","BinomRatioCI","BinToDec","BoxCox","BoxCoxInv","BoxCoxLambda","BoxedText","BreslowDayTest","BubbleLegend","Canvas","CartToPol","CartToSph","CatTable","CCC","CharToAsc",
"%c%","%like%","%like any%", "%nin%","%overlaps%","AddMonths","Agree","AllDuplicated","aovlDetails","aovlErrorTerms","as.matrix.xtabs","AscToChar","Assocs","Atkinson","AUC","AxisBreak","BartelsRankTest","BinomCI","BinomDiffCI","BinomRatioCI","BinToDec","BoxCox","BoxCoxInv","BoxCoxLambda","BoxedText","BreslowDayTest","BubbleLegend","Canvas","CartToPol","CartToSph","CatTable","CCC","CharToAsc",
"Clockwise","Closest","Coalesce","CourseData",
"CochranArmitageTest",
"CochranQTest",
Expand All @@ -22,7 +22,7 @@ export(
"HotellingsT2Test","Hour","HuberM","ICC","identify.formula","IdentifyA","Impute","InDots","Interval","IRR","IsDate","IsDichotomous","IsEuclid","IsLeapYear","IsNumeric","IsOdd","IsPrime","IsValidHwnd","IsWeekend","IsWhole","IsZero","JarqueBeraTest",
"KappaM","KendallTauB","KendallW","Keywords","KrippAlpha","Kurt",
"Label","Label<-","Labels","Labels<-","Unit","Unit<-","Lambda","Large","LastDayOfMonth","Lc","LCM","LehmacherTest",
"LeveneTest",
"LeveneTest","as.ym","NALevel",
"LillieTest","lines.Lc","lines.lm","lines.loess","lines.smooth.spline","lines.SmoothSpline","LinScale",
"LOCF","LOF","Logit","LogitInv","LogSt","LogStInv","LsFct","LsObj",
"Mar", "Mgp", "Margins","MeanAD",
Expand Down Expand Up @@ -119,6 +119,16 @@ importFrom("httr","GET","write_disk", "http_status")
importFrom("cli", "col_red","col_silver","style_bold","col_blue","col_cyan")


S3method(as.Date, ym)
S3method(Month, ym)
S3method(Month, default)
S3method(Year, ym)
S3method(Year, default)
S3method(AddMonths, ym)
S3method(AddMonths, default)
S3method(print, ym)


S3method(Desc, formula)
S3method(Desc, default)
S3method(Desc, data.frame)
Expand Down
18 changes: 18 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@

DescTools 0.99.5 (2024-03-17)
------------------------------

NEW FUNCTIONS ADDED:
* as.ym() adds a yearmonth class for extracting dateparts and adding months.
* as.Date.ym() converts a yearmonth to a date.
* NALevel() allows to quickly define an alternative level for NAs in a factor.

UPDATED FUNCTIONS:
* Year(), Month() now also extract the parts from a yearmonth representation.
* Hodges-Lehman now uses long-longs instead of integers, allowing larger vectors.
* AddMonthsYM() has been integrated in AddMonths(). Use AddMonths(as.ym(202402))
instead.

BUGFIXES:
* none




DescTools 0.99.53 (2024-01-17)
Expand Down
81 changes: 72 additions & 9 deletions R/DescTools.r
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,14 @@ TextToTable <- function(x, dimnames = NULL, check.names=FALSE, ...){
}


NALevel <- function(x, level){
# replaces NAs by the defined level in a factor x
x <- factor(x, exclude=NULL)
levels(x)[is.na(levels(x))] <- level
return(x)
}


Recode <- function(x, ..., elselevel=NA, use.empty=FALSE, num=FALSE){

# if x is character, turn it to factor and reconvert it when finished
Expand Down Expand Up @@ -3177,7 +3185,11 @@ IsWeekend <- function(x) {


# Year <- function(x){ as.integer( format(as.Date(x), "%Y") ) }
Year <- function(x){ as.POSIXlt(x)$year + 1900L }
Year <- function(x){
UseMethod("Year")
}

Year.default <- function(x){ as.POSIXlt(x)$year + 1900L }


IsLeapYear <- function(x){
Expand All @@ -3187,7 +3199,22 @@ IsLeapYear <- function(x){
}


Month <- function (x, fmt = c("m", "mm", "mmm"), lang = DescToolsOptions("lang"), stringsAsFactors = TRUE) {
Month <- function(x, fmt = c("m", "mm", "mmm"),
lang = DescToolsOptions("lang"), stringsAsFactors = TRUE) {
UseMethod("Month")
}


Month.ym <- function(x, fmt = c("m", "mm", "mmm"),
lang = DescToolsOptions("lang"), stringsAsFactors = TRUE) {
# unclass(x - Year(x) * 100)
x <- as.Date(x)
NextMethod()
}


Month.default <- function(x, fmt = c("m", "mm", "mmm"),
lang = DescToolsOptions("lang"), stringsAsFactors = TRUE) {

res <- as.POSIXlt(x)$mon + 1L

Expand Down Expand Up @@ -3365,12 +3392,6 @@ YearDay <- function(x) {
}


YearMonth <- function(x){
# returns the yearmonth representation of a date x
x <- as.POSIXlt(x)
return((x$year + 1900L)*100L + x$mon + 1L)
}


Today <- function() Sys.Date()

Expand All @@ -3397,6 +3418,42 @@ Timezone <- function(x) {
}


YearMonth <- function(x){
# returns the yearmonth representation of a date x
x <- as.POSIXlt(x)
return(as.ym((x$year + 1900L)*100L + x$mon + 1L))
}


Year.ym <- function(x){ unclass(round((x/100))) }



# define a new class ym ("yearmonth")
as.ym <- function(x){

# expects a YYYYMM format
if((y <- round(x/100)) %[]% c(1000, 3000) & (x - y*100) %[]% c(1, 12) )
structure(as.integer(x), class = c("ym", "num"))
else
structure(NA_integer_, class = c("ym", "num"))

}

print.ym <- function(x, ...) {
# do not print the class attributes
print(unclass(x), ...)
}


as.Date.ym <- function(x, d=1, ...){
as.Date(gsub("([0-9]{4})([0-9]{2})([0-9]{2})", "\\1-\\2-\\3",
x*100 + d))
}




DiffDays360 <- function(start_d, end_d, method=c("eu","us")){

# source: http://en.wikipedia.org/wiki/360-day_calendar
Expand Down Expand Up @@ -3457,7 +3514,13 @@ MonthDays <- function (x) {
}



AddMonths <- function (x, n, ...) {
UseMethod("AddMonths")
}


AddMonths.default <- function (x, n, ...) {

.addMonths <- function (x, n) {

Expand Down Expand Up @@ -3492,7 +3555,7 @@ AddMonths <- function (x, n, ...) {



AddMonthsYM <- function (x, n) {
AddMonths.ym <- function (x, n, ...) {

.addMonths <- function (x, n) {

Expand Down
2 changes: 1 addition & 1 deletion man/AddMonths.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Thanks to Antonio: \url{https://stackoverflow.com/questions/14169620/add-a-month
\author{Andri Signorell <andri@signorell.net>, based on code by Roland Rapold and Antonio
}

\seealso{\code{\link{AddMonthsYM}}; Date functions: \code{\link{Year}}, \code{\link{Month}}, etc.
\seealso{\code{\link{as.ym}}; Date functions: \code{\link{Year}}, \code{\link{Month}}, etc.
%% ~~objects to See Also as \code{\link{help}}, ~~~
}
\examples{
Expand Down
37 changes: 0 additions & 37 deletions man/AddMonthsYM.Rd

This file was deleted.

6 changes: 4 additions & 2 deletions man/DateFunctions.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
\alias{YearDays}
\alias{MonthDays}
\alias{HmsToMinute}
\alias{Month.ym}
\alias{Year.ym}

%- Also NEED an '\alias' for EACH other topic documented here.
\title{Basic Date Functions
Expand Down Expand Up @@ -88,9 +90,9 @@ MonthDays(x)

\details{These functions are mainly convenience wrappers for the painful \code{format()} and its strange codes...\cr
Based on the requested time component, the output is as follows:\cr\cr
\code{Year} returns the year of the input date in yyyy format.\cr
\code{Year} returns the year of the input date in yyyy format or a yearmonth yyyymm.\cr
\code{Quarter} returns the quarter of the year (1 to 4) for the input date. \cr
\code{Month} returns the month of the year (1 to 12) for the input date. \cr
\code{Month} returns the month of the year (1 to 12) for the input date or for a yearmonth yyyymm. \cr
\code{Week} returns the week of the year for the input date (0 to 53), as defined in ISO8601. \cr
\code{Weekday} returns the week day of the input date. (1 - Monday, 2 - Tuesday, ... 7 - Sunday). (Names and abbreviations are either english or in the current locale!)\cr
\code{YearDay} returns the day of the year numbering (1 to 366). \cr
Expand Down
2 changes: 1 addition & 1 deletion man/DescTools-package.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ _RevWeibull \tab Reverse Weibull distribution (\link{dRevWeibull})\cr
\cr\cr
\bold{Date functions:}\tab\cr
\link{day.name}, \link{day.abb} \tab Defined names of the days \cr
\link{AddMonths}, \link{AddMonthsYM} \tab Add a number of months to a given date \cr
\link{AddMonths} \tab Add a number of months to a given date \cr
\link{IsDate} \tab Check whether x is a date object \cr
\link{IsWeekend} \tab Check whether x falls on a weekend \cr
\link{IsLeapYear} \tab Check whether x is a leap year \cr
Expand Down
49 changes: 49 additions & 0 deletions man/NALevel.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
\name{NALevel}
\alias{NALevel}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{Replace NAs in a Factor by a Given Level
%% ~~function to do ... ~~
}
\description{In order to replace the NAs in a factor an additional level has to be defined first.
This function does this and replaces the NAs by the given level.
%% ~~ A concise (1-5 lines) description of what the function does. ~~
}
\usage{
NALevel(x, level)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{x}{a vector which will be turned into a factor.
%% ~~Describe \code{x} here~~
}
\item{level}{the name for the new level
%% ~~Describe \code{level} here~~
}
}
\value{the vector x with the NAs replaced by level
%% ~Describe the value returned
%% If it is a LIST, use
%% \item{comp1 }{Description of 'comp1'}
%% \item{comp2 }{Description of 'comp2'}
%% ...
}
\author{Andri Signorell <andri@signorell.net>
%% ~~who you are~~
}
\seealso{\code{\link{factor}}, \code{\link{levels}}
%% ~~objects to See Also as \code{\link{help}}, ~~~
}
\examples{
x <- c(LETTERS[1:5], NA)
table(NALevel(x, "something else"))

}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory (show via RShowDoc("KEYWORDS")):
% \keyword{ ~kwd1 }
% \keyword{ ~kwd2 }
% Use only one keyword per line.
% For non-standard keywords, use \concept instead of \keyword:
% \concept{ ~cpt1 }
% \concept{ ~cpt2 }
% Use only one concept per line.
58 changes: 58 additions & 0 deletions man/as.ym.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
\name{as.ym}
\alias{as.ym}
\alias{as.Date.ym}
\alias{AddMonths.ym}

\title{A Class for Dealing with the Yearmonth Format
}
\description{
The representation of year and month information in YYYYYMM format as an integer is often handy and a useful and efficient data structure. Adding a number of months to such a date is not quite catchy, however, since the date structure is to be retained. For example, 201201 - 2 [months] is expected to result in 201111 instead of 201199. AddMonthsYM does this job.
}
\usage{

as.ym(x)
\method{as.Date}{ym}(x, d = 1, ...)

\method{AddMonths}{ym}(x, n, ...)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{x}{a vector of integers, representing the dates in the format YYYYMM, to which a number of months has to be added. YYYY must lie in the range of 1000-3000, MM in 1-12.
}
\item{d}{the day to be used for converting a yearmonth to a date. Default is 1.
%% ~~Describe \code{n} here~~
}
\item{n}{the number of months to be added. If n is negative the months will be subtracted.
%% ~~Describe \code{n} here~~
}
\item{\dots}{further arguments (not used here).}
}
\details{All parameters will be recyled if necessary. The therefore used function \code{\link{mapply}} will display a warning, if the longer argument is not a multiple of the length of the shorter one.
}
\value{a vector of class \code{integer} with the same dimension as x, containing the transformed dates.
}
\author{Andri Signorell <andri@signorell.net>, originally based on code by Roland Rapold
}

\seealso{\code{\link{AddMonths}}; Date functions, like \code{\link{Year}}, \code{\link{Month}}, etc.
%% ~~objects to See Also as \code{\link{help}}, ~~~
}
\examples{

Month(as.ym(202408))
Year(as.ym(202408))

Year(as.Date("2024-12-05"))
Year(as.ym(202412))

Month(as.Date("2024-12-05"), fmt = "mm")
Month(as.ym(202412), fmt="mm")

AddMonths(201511, 5)

AddMonths(c(201511, 201302), c(5, 15))
AddMonths(c(201511, 201302), c(5, -4))
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ chron }
5 changes: 4 additions & 1 deletion pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ reference:

# Transform: factor to factor
- Recode
- NALevel

# Transform: factor to numeric
- Dummy
Expand Down Expand Up @@ -156,7 +157,9 @@ reference:
- day.name
- day.abb
- AddMonths
- AddMonthsYM
- as.ym
- YearFromYM
- MonthFromYM
- IsDate
- IsWeekend
- IsLeapYear
Expand Down
Binary file modified src/DescTools.dll
Binary file not shown.
Binary file modified src/aux_fct.o
Binary file not shown.
Loading

0 comments on commit ef135a3

Please sign in to comment.