Title: | Kirill's Miscellaneous Functions |
---|---|
Description: | A collection of useful functions not found anywhere else, mainly for programming: Pretty intervals, generalized lagged differences, checking containment in an interval, and an alternative interface to assign(). |
Authors: | Kirill Müller [aut, cre] |
Maintainer: | Kirill Müller <[email protected]> |
License: | GPL-3 |
Version: | 0.4 |
Built: | 2024-10-15 04:30:26 UTC |
Source: | https://github.com/krlmlr/kimisc |
A collection of useful functions not found anywhere else, mainly for programming: Pretty intervals, generalized lagged differences, checking containment in an interval, and an alternative interface to assign().
Kirill Müller
Useful links:
This (vectorized) function returns the first
non-NA
argument, similar to the SQL function
COALESCE
. If a vector or matrix is passed as first argument,
the remaining arguments are recycled to generate a vector/matrix of
the same dimension, and coalescing is done element by element.
coalesce.na(x, ...)
coalesce.na(x, ...)
x |
The first value to coalesce. |
... |
Other values to coalesce. |
A vector of the same length as x
.
Other deprecated functions: df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
coalesce.na(NA, -1) coalesce.na(5, 3) coalesce.na(c(1,NA,NA), c(NA,2)) coalesce.na(matrix(c(NA, 1:3), nrow=2)) coalesce.na(NA)
coalesce.na(NA, -1) coalesce.na(5, 3) coalesce.na(c(1,NA,NA), c(NA,2)) coalesce.na(matrix(c(NA, 1:3), nrow=2)) coalesce.na(NA)
This is an enhanced version of base::cut()
that allows a custom
formatting to be applied to the values.
cut_format(x, breaks, include.lowest = FALSE, right = TRUE, ordered_result = FALSE, ..., format_fun = format, sep = ", ", paren = c("(", "[", ")", "]"))
cut_format(x, breaks, include.lowest = FALSE, right = TRUE, ordered_result = FALSE, ..., format_fun = format, sep = ", ", paren = c("(", "[", ")", "]"))
x |
a numeric vector which is to be converted to a factor by cutting. |
breaks |
|
include.lowest |
logical, indicating if an ‘x[i]’ equal to
the lowest (or highest, for |
right |
logical, indicating if the intervals should be closed on the right (and open on the left) or vice versa. |
ordered_result |
logical: should the result be an ordered factor? |
... |
Passed to |
format_fun |
|
sep |
|
paren |
|
http://stackoverflow.com/q/14456371/946850
cut_format(runif(10), seq(0, 1, by = 0.25), format_fun = function(x) paste(x * 100, "%")) cut_format(runif(10), seq(0, 1, by = 0.25), paren = c("<", "{", ">", "}"))
cut_format(runif(10), seq(0, 1, by = 0.25), format_fun = function(x) paste(x * 100, "%")) cut_format(runif(10), seq(0, 1, by = 0.25), paren = c("<", "{", ">", "}"))
This function converts a data frame back to a list. It is the reverse operation to list_to_df.
df_to_list(df_for_list)
df_to_list(df_for_list)
df_for_list |
The data frame to be converted to a list |
In a data frame with more than two columns, heuristics are applied to detect the name and value column.
Other deprecated functions: coalesce.na-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
This function is a wrapper around export.list()
that
exports variables by their name to another environment.
export(..., target.env = .GlobalEnv)
export(..., target.env = .GlobalEnv)
... |
variables to be exported. |
target.env |
The target environment. Use the global environment by default. |
Invisible NULL
.
Roland
http://stackoverflow.com/a/17484932/946850
local({ newly.created.var <- 5 export(newly.created.var) }) newly.created.var rm(newly.created.var)
local({ newly.created.var <- 5 export(newly.created.var) }) newly.created.var rm(newly.created.var)
This function is a wrapper around assign()
that
exports the contents of a named list to an environment. The variable names
in the target environment are constructed from the names of the list items
or taken from a separate argument.
export.list(arg.list, arg.names = names(arg.list), target.env = .GlobalEnv)
export.list(arg.list, arg.names = names(arg.list), target.env = .GlobalEnv)
arg.list |
list of objects, possibly named. |
arg.names |
names to use for the items in the target environment. Use
the names of |
target.env |
The target environment. Use the global environment by default. |
Invisible NULL
.
Roland
http://stackoverflow.com/a/17484932/946850
export.list(list(newly.created.var=5)) newly.created.var rm(newly.created.var)
export.list(list(newly.created.var=5)) newly.created.var rm(newly.created.var)
Returns suitably lagged and iterated differences using arbitrary difference functions.
gdiff(x, lag = 1L, differences = 1L, FUN = `-`, ...)
gdiff(x, lag = 1L, differences = 1L, FUN = `-`, ...)
x |
a numeric vector or matrix containing the values to be differenced. |
lag |
an integer indicating which lag to use. |
differences |
an integer indicating the order of the difference. |
FUN |
A distance function that accepts two parameters |
... |
further arguments to be passed to or from methods. |
If x is a vector of length n
and differences = 1
, then
the computed result is equal to the successive differences
FUN(x[(1+lag):n], x[1:(n-lag)])
.
If difference
is larger than one this algorithm is applied
recursively to x
. Note that the returned value is a vector which is
shorter than x
.
If x
is a matrix then the difference operations are carried out on each
column separately.
gdiff(1:4) gdiff(1:4, FUN = `/`)
gdiff(1:4) gdiff(1:4, FUN = `/`)
This function is very similar to strptime
with the
%X
conversion specification. Anything with three numbers between
two colons is interpreted as a time, no consistency check is performed on
the actual hour, minute and second values. Thus, strings like 25:15:00 and
23:78:101 also will be converted. Incorrectly formatted strings are
converted to NA
with a warning.
hms.to.seconds(x)
hms.to.seconds(x)
x |
A (vector of) strings in H:M:S format. |
A (vector of) integer values of the same length as x
.
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
hms.to.seconds(c("00:00:01", "00:01:00", "01:00:00")) hms.to.seconds(c("25:15:00", "23:78:101")) hms.to.seconds("invalid")
hms.to.seconds(c("00:00:01", "00:01:00", "01:00:00")) hms.to.seconds(c("25:15:00", "23:78:101")) hms.to.seconds("invalid")
This function checks if the values in the x
parameter are contained in the interval (lo
, hi
].
NA
values are treated as "not in the interval".
in.interval.lo(x, lo, hi)
in.interval.lo(x, lo, hi)
x |
A vector of values. (Lists will be coerced to a numeric vector.) |
lo |
Left end of the interval. |
hi |
Right end of the interval. |
A boolean vector of the same length as x
.
in.interval.ro, nin.interval.lo, nin.interval.ro
in.interval.lo(c(-1, 0, 1, 2), 0, 1) in.interval.lo(NA, 1, 3)
in.interval.lo(c(-1, 0, 1, 2), 0, 1) in.interval.lo(NA, 1, 3)
This function checks if the values in the x
parameter are contained in the interval [lo
, hi
).
NA
values are treated as "not in the interval".
in.interval.ro(x, lo, hi)
in.interval.ro(x, lo, hi)
x |
A vector of values. (Lists will be coerced to a numeric vector.) |
lo |
Left end of the interval. |
hi |
Right end of the interval. |
A boolean vector of the same length as x
.
in.interval.lo, nin.interval.lo, nin.interval.ro
in.interval.ro(c(-1, 0, 1, 2), 0, 1) in.interval.ro(NA, 1, 3)
in.interval.ro(c(-1, 0, 1, 2), 0, 1) in.interval.ro(NA, 1, 3)
The "See also" section contains the deprecated functions in this package.
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
This function coerces its input to a list and returns a data frame with as many rows as there are list items in the input, and two columns (one for the names, one for the values). If the list is not named, the natural sequence will be used as item names.
list_to_df(list_for_df)
list_to_df(list_for_df)
list_for_df |
The object to be converted to a data frame |
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
This function is a wrapper around c()
that
assigns names to unnamed arguments based on the unevaluated expression used
in the call.
nc(...)
nc(...)
... |
Vector elements, possibly named |
A named vector.
Hadley Wickham
http://stackoverflow.com/a/5043280/946850, http://tolstoy.newcastle.edu.au/R/e9/help/10/03/8392.html
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
a <- 1; b <- 2; c <- 3 nc(a, b, d=c) nc(mean(c(a, b, c)))
a <- 1; b <- 2; c <- 3 nc(a, b, d=c) nc(mean(c(a, b, c)))
This function checks if the values in the x
parameter are contained in the interval (lo
, hi
].
NA
values are treated as "not in the interval".
nin.interval.lo(x, lo, hi)
nin.interval.lo(x, lo, hi)
x |
A vector of values. (Lists will be coerced to a numeric vector.) |
lo |
Left end of the interval. |
hi |
Right end of the interval. |
A boolean vector of the same length as x
.
in.interval.lo, in.interval.ro, nin.interval.ro
nin.interval.lo(c(-1, 0, 1, 2), 0, 1) nin.interval.lo(NA, 1, 3)
nin.interval.lo(c(-1, 0, 1, 2), 0, 1) nin.interval.lo(NA, 1, 3)
This function checks if the values in the x
parameter are contained in the interval [lo
, hi
).
NA
values are treated as "not in the interval".
nin.interval.ro(x, lo, hi)
nin.interval.ro(x, lo, hi)
x |
A vector of values. (Lists will be coerced to a numeric vector.) |
lo |
Left end of the interval. |
hi |
Right end of the interval. |
A boolean vector of the same length as x
.
in.interval.lo, in.interval.ro, nin.interval.lo
nin.interval.ro(c(-1, 0, 1, 2), 0, 1) nin.interval.ro(NA, 1, 3)
nin.interval.ro(c(-1, 0, 1, 2), 0, 1) nin.interval.ro(NA, 1, 3)
This function is a wrapper around list()
that
assigns names to unnamed arguments based on the unevaluated expression used
in the call.
nlist(...)
nlist(...)
... |
List items, possibly named |
A named list.
Hadley Wickham
http://stackoverflow.com/a/5043280/946850, http://tolstoy.newcastle.edu.au/R/e9/help/10/03/8392.html
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
a <- 1; b <- 2; c <- 3 nlist(a, b, d=c) nlist(mean(c(a, b, c)))
a <- 1; b <- 2; c <- 3 nlist(a, b, d=c) nlist(mean(c(a, b, c)))
The function ofactor()
is a convenience wrapper for
factor()
that orders the levels as they appear in the data if the
levels
argument is not specified.
ofactor(x = character(), ...)
ofactor(x = character(), ...)
x |
A vector of data, usually taking a small number of distinct values. |
... |
Other arguments passed on to |
A factor. See base::factor()
for details.
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
ofactor(3:1) ofactor(9:12, exclude=11) identical(ofactor(3:1, levels=1:3), factor(3:1))
ofactor(3:1) ofactor(9:12, exclude=11) identical(ofactor(3:1, levels=1:3), factor(3:1))
This function takes a sample of the specified size
from the rows of x
using either with or without replacement.
sample.rows(x, size, replace = FALSE, prob = NULL)
sample.rows(x, size, replace = FALSE, prob = NULL)
x |
A data frame. |
size |
A non-negative integer giving the number of items to choose. |
replace |
Should sampling be with replacement? |
prob |
A vector of probability weights for obtaining the rows of the data frame being sampled. |
This function internally calls sample.int()
.
A data frame of the same shape as x
.
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
set.seed(42) sample.rows(data.frame(a=c(1,2,3), b=c(4,5,6), row.names=c('a', 'b', 'c')), 10, replace=TRUE)
set.seed(42) sample.rows(data.frame(a=c(1,2,3), b=c(4,5,6), row.names=c('a', 'b', 'c')), 10, replace=TRUE)
This function is very similar to strftime
with the
%X
conversion specification. Hour values larger than 24 are
permitted. Fractions will be rounded down to the next integer. Non-numeric
values are coerced to NA
with a warning.
seconds.to.hms(x)
seconds.to.hms(x)
x |
A (vector of) numbers. |
A (vector of) character values of the same length as x
.
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
thisfile-deprecated
,
tll-deprecated
,
vswitch-deprecated
seconds.to.hms(c(1, 60, 3600.5)) seconds.to.hms(c(100000, -4000.5)) seconds.to.hms("invalid")
seconds.to.hms(c(1, 60, 3600.5)) seconds.to.hms(c(100000, -4000.5)) seconds.to.hms("invalid")
R does not store nor export the path of the currently running script. This is an attempt to circumvent this limitation by applying heuristics (such as call stack and argument inspection) that work in many cases.
thisfile() thisfile_source() thisfile_r() thisfile_rscript() thisfile_knit()
thisfile() thisfile_source() thisfile_r() thisfile_rscript() thisfile_knit()
This functions currently work only if the script was source
d,
processed with knitr
,
or run with Rscript
or using the --file
parameter to the
R
executable. For code run with Rscript
, the exact value
of the parameter passed to Rscript
is returned.
The path of the currently running script, NULL if it cannot be determined.
Kirill Müller, Hadley Wickham, Michael R. Head
http://stackoverflow.com/q/1815606/946850
base::source()
, utils::Rscript()
, base::getwd()
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
tll-deprecated
,
vswitch-deprecated
## Not run: thisfile()
## Not run: thisfile()
The argument is assumed to be a list of (named) lists
with length
each. It is converted to a (named) list of
elements with length
each.
tll(l)
tll(l)
l |
List of lists, possibly named. |
A list of lists corresponding to a transposition of the argument.
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
vswitch-deprecated
tll(list(list(1, 2), list(3, 4))) tll(list(list(a=1, b=2), list(a=3, b=4))) tll(list(x=list(a=1, b=2), y=list(a=3, b=4)))
tll(list(list(1, 2), list(3, 4))) tll(list(list(a=1, b=2), list(a=3, b=4))) tll(list(x=list(a=1, b=2), y=list(a=3, b=4)))
The function vswitch
is a vectorized version of
base::switch()
optimized for performance.
vswitch(EXPR, ...)
vswitch(EXPR, ...)
EXPR |
an expression evaluating to a number or a character string. |
... |
the list of alternatives. If it is intended that
|
Only the EXPR
argument is treated as a vector. In particular,
if any of the alternatives (or the default alternative) is a vector, the
result will be a list of vectors.
The value of one of the elements of ...
,
or NA
whenever no element is selected.
Contrary to base::switch()
the result is
always visible.
Other deprecated functions: coalesce.na-deprecated
,
df_to_list-deprecated
,
hms.to.seconds-deprecated
,
kimisc-deprecated
,
list_to_df-deprecated
,
nc-deprecated
,
nlist-deprecated
,
ofactor-deprecated
,
sample.rows-deprecated
,
seconds.to.hms-deprecated
,
thisfile-deprecated
,
tll-deprecated