# NOT RUN {
## First five roman 'numbers'.
(y <- as.roman(1 : 5))
## Middle one.
y[3]
## Current year as a roman number.
(y <- as.roman(format(Sys.Date(), "%Y")))
## Today, and 10, 20, 30, and 100 years ago ...
y - 10*c(0:3,10)
# }
# NOT RUN {
## mixture of arabic and roman numbers :
as.roman(c(NA, 1:3, "", strrep("I", 1:6))) # + NA with a warning for "IIIIII"
cc <- c(NA, 1:3, strrep("I", 0:5))
(rc <- as.roman(cc)) # two NAs: 0 is not "roman"
(ic <- as.integer(rc)) # works automatically [without an explicit method]
rNA <- as.roman(NA)
## simple consistency checks -- arithmetic when result is in {1,2,..,3899} :
stopifnot(identical(rc, as.roman(rc)), # as.roman(.) is "idempotent"
identical(rc + rc + (3*rc), rc*5),
identical(ic, c(NA, 1:3, NA, 1:5)),
identical(as.integer(5*rc), 5L*ic),
identical(as.numeric(rc), as.numeric(ic)),
identical(rc[1], rNA),
identical(as.roman(0), rNA),
identical(as.roman(NA_character_), rNA),
identical(as.list(rc), as.list(ic)))
## Non-Arithmetic 'Ops' :
stopifnot(exprs = {
# Comparisons :
identical(ic < 1:5, rc < 1:5)
identical(ic < 1:5, rc < as.roman(1:5))
# Logic [integers |-> logical] :
identical(rc & TRUE , ic & TRUE)
identical(rc & FALSE, ic & FALSE)
identical(rc | FALSE, ic | FALSE)
identical(rc | NA , ic | NA)
})
## 'Summary' group functions (and comparison):
(rc. <- rc[!is.na(rc)])
stopifnot(exprs = {
identical(min(rc), as.roman(NA))
identical(min(rc, na.rm=TRUE),
as.roman(min(ic, na.rm=TRUE)))
identical(range(rc.),
as.roman(range(as.integer(rc.))))
identical(sum (rc, na.rm=TRUE), as.roman("XXI"))
identical(format(prod(rc, na.rm=TRUE)), "DCCXX")
format(prod(rc.)) == "DCCXX"
})
# }
Run the code above in your browser using DataLab