Title: | Misc Functions of the Center for Mathematical Research |
---|---|
Description: | A collection of useful helper routines developed by students of the Center for Mathematical Research, Stankin, Moscow. |
Authors: | Andrey Paramonov |
Maintainer: | Andrey Paramonov <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.3.1 |
Built: | 2025-02-08 04:52:06 UTC |
Source: | https://github.com/aparamon/cmrutils |
Creates empty copy of object.
empty(x)
empty(x)
x |
An object to create copy from. |
Returns empty (filled with NA
s) object having the same
structure and attributes as x
.
empty(0) empty(1:10) empty(matrix(1:4, 2, 2)) empty(array(1:24, dim = c(2, 3, 4))) empty(list(a = 1, b = 2)) empty(data.frame(a = 1:2, b = 3:4)) empty(ts(1:10, frequency = 5))
empty(0) empty(1:10) empty(matrix(1:4, 2, 2)) empty(array(1:24, dim = c(2, 3, 4))) empty(list(a = 1, b = 2)) empty(data.frame(a = 1:2, b = 3:4)) empty(ts(1:10, frequency = 5))
Sets up graphical device and plots figure. For PDF device, also takes care of embedding fonts (see Details).
makefig(plotfun, ..., device = c('default', 'pdf', 'svg'), width = 8, height = 6, scale = pointsize/12, pointsize = 12, filename = 'Rplot', family = 'Helvetica')
makefig(plotfun, ..., device = c('default', 'pdf', 'svg'), width = 8, height = 6, scale = pointsize/12, pointsize = 12, filename = 'Rplot', family = 'Helvetica')
plotfun |
A function which does the actual plotting. |
... |
Additional arguments passed to |
device |
The type of graphical device. |
width , height
|
The unscaled width and height of the graphics region in
inches. Actual size is |
scale |
The scaling factor. Is 1 for the default |
pointsize |
The base font size in points. |
filename |
The filename for PDF or SVG device. If "standard" extension is missing, it is added automagically. |
family |
The font family for PDF device (see Details). |
For PDF device, function does font embedding via call to
embedFonts
. The package provides additional font
families "CMRoman"
, "CMSans"
which work nicely with
cyrillics. These families are based on CM-Super font package by
Vladimir Volovich and CMSYASE symbol font by Paul Murrell.
Both metric (‘*.afm’) and outline (‘*.pfb’) files for the
"CMRoman"
, "CMSans"
fonts are provided with the
package. This allows to embed these fonts even if they are not
installed in the system.
Returns the result value of call to plotfun
invisibly.
Paul Murrell (2005?) Using Computer Modern Fonts in R Graphics. http://www.stat.auckland.ac.nz/~paul/R/CM/CMR.html
x <- rnorm(10) myplot <- function(x, legend, ...) { plot(x, ..., pch = 1) legend('bottomright', bg = 'white', legend = legend, pch = 1) } makefig(myplot, x = x, legend = 'Simulation') makefig(myplot, x = x, legend = 'Simulation', pointsize = 8) makefig(myplot, x = x, legend = 'Simulation', device = 'pdf', filename = 'embedding.pdf', family = 'CMSans') mapply(makefig, device = c('window', 'pdf', 'svg'), MoreArgs = list(plotfun = myplot, x = x, legend = 'Simulation', filename = 'test', family = 'CMSans'))
x <- rnorm(10) myplot <- function(x, legend, ...) { plot(x, ..., pch = 1) legend('bottomright', bg = 'white', legend = legend, pch = 1) } makefig(myplot, x = x, legend = 'Simulation') makefig(myplot, x = x, legend = 'Simulation', pointsize = 8) makefig(myplot, x = x, legend = 'Simulation', device = 'pdf', filename = 'embedding.pdf', family = 'CMSans') mapply(makefig, device = c('window', 'pdf', 'svg'), MoreArgs = list(plotfun = myplot, x = x, legend = 'Simulation', filename = 'test', family = 'CMSans'))
Replaces non-finite values with NA
s in array-like objects.
nan2na(x)
nan2na(x)
x |
An array-like object. |
Returns x
with non-finite values replaced with NA
s.
nan2na(Inf) nan2na(rep(c(0, -Inf, Inf), 3)) nan2na(matrix(c(0, Inf, -Inf, 0), 2, 2)) nan2na(array(c(0, -Inf, Inf, 1, NaN), dim = c(2, 3, 4))) nan2na(ts(rep(c(0, -Inf, Inf), 2), frequency = 5))
nan2na(Inf) nan2na(rep(c(0, -Inf, Inf), 3)) nan2na(matrix(c(0, Inf, -Inf, 0), 2, 2)) nan2na(array(c(0, -Inf, Inf, 1, NaN), dim = c(2, 3, 4))) nan2na(ts(rep(c(0, -Inf, Inf), 2), frequency = 5))
Slices array by specified dimension.
slice(x, MARGIN, n)
slice(x, MARGIN, n)
x |
An array-like object. |
MARGIN |
An integer giving the dimension to slice by. |
n |
Number of slice. |
Returns array of dimensions dim(x)[-MARGIN]
. The corresponding
dimnames are preserved.
x <- 1:10 slice(x, 1, 1) x <- matrix(1:4, 2, 2) rownames(x) <- c("Row 1", "Row 2") colnames(x) <- c("Col 1", "Col 2") x slice(x, 1, 1) slice(x, 1, 2) slice(x, 2, 1) slice(x, 2, 2) x <- array(1:24, dim = c(2, 3, 4)) dimnames(x) <- list(letters[1:2], letters[3:5], letters[6:9]) x slice(x, 1, 1) slice(x, 2, 1) slice(x, 3, 1)
x <- 1:10 slice(x, 1, 1) x <- matrix(1:4, 2, 2) rownames(x) <- c("Row 1", "Row 2") colnames(x) <- c("Col 1", "Col 2") x slice(x, 1, 1) slice(x, 1, 2) slice(x, 2, 1) slice(x, 2, 2) x <- array(1:24, dim = c(2, 3, 4)) dimnames(x) <- list(letters[1:2], letters[3:5], letters[6:9]) x slice(x, 1, 1) slice(x, 2, 1) slice(x, 3, 1)
Converts beetween Date
objects and sampling times of
daily time series with the natural time period of 1 year.
time365toDate(x) time365fromDate(x)
time365toDate(x) time365fromDate(x)
x |
A vector of sampling times ( |
A simple approach to modelling daily time series with the natural time
period of 1 year is to use ts
object with
, that is to assume no leap days.
time365fromDate
returns NA
for the leap day February 29.
A vector of dates (time365fromDate
) or sampling times
(time365toDate
).
time
.
## non-leap year x <- ts(1:4, start = c(1991, 58), frequency = 365) time365toDate(time(x)) d <- seq.Date(as.Date('1991-02-27'), as.Date('1991-03-02'), 1) time365fromDate(d) ## leap year x <- ts(1:4, start = c(1992, 58), frequency = 365) time365toDate(time(x)) d <- seq.Date(as.Date('1992-02-27'), as.Date('1992-03-02'), 1) time365fromDate(d) x <- seq(1900, 2100, by = 1/365) stopifnot(all(abs(x - time365fromDate(time365toDate(x))) < getOption("ts.eps")))
## non-leap year x <- ts(1:4, start = c(1991, 58), frequency = 365) time365toDate(time(x)) d <- seq.Date(as.Date('1991-02-27'), as.Date('1991-03-02'), 1) time365fromDate(d) ## leap year x <- ts(1:4, start = c(1992, 58), frequency = 365) time365toDate(time(x)) d <- seq.Date(as.Date('1992-02-27'), as.Date('1992-03-02'), 1) time365fromDate(d) x <- seq(1900, 2100, by = 1/365) stopifnot(all(abs(x - time365fromDate(time365toDate(x))) < getOption("ts.eps")))