Title: | Miscellaneous Basic Functions |
---|---|
Description: | A collection of miscellaneous functions for copying objects to the clipboard ('Copy'); manipulating strings ('concat', 'mgsub', 'trim', 'verlan'); loading or showing packages ('library_with_dep', 'require_with_dep', 'sessionPackages'); creating or testing for named lists ('nlist', 'as_nlist', 'is_nlist'), formulas ('is_formula'), empty objects ('as_empty', 'is_empty'), whole numbers ('as_wholenumber', 'is_wholenumber'); testing for equality ('almost_equal', 'almost_zero') and computing uniqueness ('almost_unique'); getting modified versions of usual functions ('rle2', 'sumNA'); making a pause or a stop ('pause', 'stopif'); converting into a function ('as_fun'); providing a C like ternary operator ('condition %?% true %:% false'); finding packages and functions ('get_all_pkgs', 'get_all_funs'); and others ('erase', '%nin%', 'unwhich', 'top', 'bot', 'normalize'). |
Authors: | Paul Poncet [aut, cre] |
Maintainer: | Paul Poncet <[email protected]> |
License: | GPL-3 |
Version: | 1.1.0 |
Built: | 2024-10-31 16:33:00 UTC |
Source: | https://github.com/paulponcet/bazar |
This is a C like ternary operator, the syntax being
condition %?% true %:% false
.
condition %?% true lhs %:% false
condition %?% true lhs %:% false
condition |
logical. A vector. |
true , false
|
Values to use for |
lhs |
Left-hand side of |
If length(x) > 1
, then ifelse
is used.
Richie Cotton, see https://stackoverflow.com/a/8791496/3902976; Paul Poncet for the small modifications introduced.
(capitalize <- sample(c(TRUE, FALSE), 1)) capitalize %?% LETTERS[1:3] %:% letters[1:2] # Does not work ## Not run: capitalize %?% 1*1:3 %:% 1:2 ## End(Not run) # Does work capitalize %?% {1*1:3} %:% 1:2 # Does work too capitalize %?% (1*1:3) %:% 1:2 # Vectorized version also works c(capitalize,!capitalize) %?% "A" %:% c("b","c") # Chaining operators is permitted FALSE %?% "a" %:% (FALSE %?% "b") %:% (capitalize %?% "C") %:% "c"
(capitalize <- sample(c(TRUE, FALSE), 1)) capitalize %?% LETTERS[1:3] %:% letters[1:2] # Does not work ## Not run: capitalize %?% 1*1:3 %:% 1:2 ## End(Not run) # Does work capitalize %?% {1*1:3} %:% 1:2 # Does work too capitalize %?% (1*1:3) %:% 1:2 # Vectorized version also works c(capitalize,!capitalize) %?% "A" %:% c("b","c") # Chaining operators is permitted FALSE %?% "a" %:% (FALSE %?% "b") %:% (capitalize %?% "C") %:% "c"
The function %nin%
is the negation
of the function %in%
.
x %nin% table
x %nin% table
x |
vector or NULL: the values to be matched. |
table |
vector or NULL: the values to be matched against. |
A logical vector, indicating if a non-match was located for each element of x: thus the values are TRUE or FALSE and never NA.
1:10 %nin% c(1,3,5,9)
1:10 %nin% c(1,3,5,9)
The function almost_equal
tests if two numeric vectors
have equal values up to a tolerance.
almost_equal(x, y, tolerance = sqrt(.Machine$double.eps)) almost.equal(x, y, tolerance = sqrt(.Machine$double.eps))
almost_equal(x, y, tolerance = sqrt(.Machine$double.eps)) almost.equal(x, y, tolerance = sqrt(.Machine$double.eps))
x |
numeric vector. |
y |
numeric vector of the same length as |
tolerance |
numeric. Differences smaller than tolerance are considered as equal.
The default value is close to |
A logical vector of the same length as x
and y
.
Tommy on StackOverflow, see http://stackoverflow.com/a/7667703.
almost_equal(x = 1:3, y = 1:3 + c(10^(-6), 10^(-7), 10^(-8)))
almost_equal(x = 1:3, y = 1:3 + c(10^(-6), 10^(-7), 10^(-8)))
The function almost_unique
extracts elements of a vector x
that are unique up to a tolerance factor.
almost_unique(x, ...) almost.unique(x, ...) ## Default S3 method: almost_unique(x, tolerance = sqrt(.Machine$double.eps), ...)
almost_unique(x, ...) almost.unique(x, ...) ## Default S3 method: almost_unique(x, tolerance = sqrt(.Machine$double.eps), ...)
x |
numeric. The vector of numeric values at stake. |
... |
Additional arguments to be passed to the function
|
tolerance |
numeric. Relative differences smaller than tolerance are considered as equal.
The default value is close to |
A vector of the same type as x
.
almost_unique(c(1, 1.01), tol = 0.1) almost_unique(c(1, 1.01), tol = 0.01) almost_unique(c(1, 2, 3), tol = 10) almost_unique(c(1, 2, 3), tol = 5) almost_unique(c(1, 2, 3), tol = 1)
almost_unique(c(1, 1.01), tol = 0.1) almost_unique(c(1, 1.01), tol = 0.01) almost_unique(c(1, 2, 3), tol = 10) almost_unique(c(1, 2, 3), tol = 5) almost_unique(c(1, 2, 3), tol = 1)
The function almost_zero
tests if values of
the numeric vector x
are equal to zero up
to a tolerance.
almost_zero(x, tolerance = sqrt(.Machine$double.eps)) almost.zero(x, tolerance = sqrt(.Machine$double.eps))
almost_zero(x, tolerance = sqrt(.Machine$double.eps)) almost.zero(x, tolerance = sqrt(.Machine$double.eps))
x |
numeric. The vector of numeric values at stake. |
tolerance |
numeric. Differences smaller than tolerance are considered as equal.
The default value is close to |
A logical vector of the same length as x
.
almost_zero(c(0, 10^(-7), 10^(-8)))
almost_zero(c(0, 10^(-7), 10^(-8)))
Convert x
to an empty object.
as_empty(x, ...) as.empty(x, ...) ## Default S3 method: as_empty(x, ...) ## S3 method for class 'data.frame' as_empty(x, ...)
as_empty(x, ...) as.empty(x, ...) ## Default S3 method: as_empty(x, ...) ## S3 method for class 'data.frame' as_empty(x, ...)
x |
An object. |
... |
Additional parameterS. |
An empty object
is_empty
in this package.
x <- c("a", "b", "c") as_empty(x) class(as_empty(x)) # still a character x <- factor(LETTERS) as_empty(x) # levels are kept class(as_empty(x)) # still a factor x <- data.frame(x = 1:3, y = 2:4) as_empty(x)
x <- c("a", "b", "c") as_empty(x) class(as_empty(x)) # still a character x <- factor(LETTERS) as_empty(x) # levels are kept class(as_empty(x)) # still a factor x <- data.frame(x = 1:3, y = 2:4) as_empty(x)
as_fun
is a generic function that does the same as as.function
from package base, with the additional feature that
as_fun.character
converts a string into the function it names.
as_fun(x, ...) as.fun(x, ...) ## Default S3 method: as_fun(x, envir = parent.frame(), ...) ## S3 method for class 'character' as_fun(x, ...) ## S3 method for class 'formula' as_fun(x, ...) ## S3 method for class 'name' as_fun(x, ...) ## S3 method for class 'call' as_fun(x, ...) ## S3 method for class 'numeric' as_fun(x, ...) ## S3 method for class 'logical' as_fun(x, ...) ## S3 method for class 'factor' as_fun(x, ...) ## S3 method for class 'complex' as_fun(x, ...) ## S3 method for class 'data.frame' as_fun(x, ...) ## S3 method for class 'lm' as_fun(x, ...) ## S3 method for class 'rpart' as_fun(x, ...)
as_fun(x, ...) as.fun(x, ...) ## Default S3 method: as_fun(x, envir = parent.frame(), ...) ## S3 method for class 'character' as_fun(x, ...) ## S3 method for class 'formula' as_fun(x, ...) ## S3 method for class 'name' as_fun(x, ...) ## S3 method for class 'call' as_fun(x, ...) ## S3 method for class 'numeric' as_fun(x, ...) ## S3 method for class 'logical' as_fun(x, ...) ## S3 method for class 'factor' as_fun(x, ...) ## S3 method for class 'complex' as_fun(x, ...) ## S3 method for class 'data.frame' as_fun(x, ...) ## S3 method for class 'lm' as_fun(x, ...) ## S3 method for class 'rpart' as_fun(x, ...)
x |
The object to convert. |
... |
Additional arguments (currently not used). |
envir |
Environment in which the function should be defined. |
The desired function.
as_fun.character
is adapted from MrFlick,
see https://stackoverflow.com/a/38984214 on StackOverflow.
as_function
in package rlang.
as_fun(mean) as_fun("mean") as_fun("edit") as_fun("stats::predict") ## Uses 'rlang::as_function()' for formulas under the hood: f <- as_fun(~ . + 1) f(10) # 11 ## the constant function '1' f <- as_fun(1) f(2) # 1 f("a") # 1 ## the constant function 'FALSE' f <- as_fun(FALSE) f(2) # FALSE f("a") # FALSE f <- as_fun(data.frame(x = 1:2, y = 2:3)) f("x") # 'x' column f("y") # 'y' column
as_fun(mean) as_fun("mean") as_fun("edit") as_fun("stats::predict") ## Uses 'rlang::as_function()' for formulas under the hood: f <- as_fun(~ . + 1) f(10) # 11 ## the constant function '1' f <- as_fun(1) f(2) # 1 f("a") # 1 ## the constant function 'FALSE' f <- as_fun(FALSE) f(2) # FALSE f("a") # FALSE f <- as_fun(data.frame(x = 1:2, y = 2:3)) f("x") # 'x' column f("y") # 'y' column
These methods transform values to NA
for different classes of objects.
as_na(x, ...) as.na(x, ...) ## Default S3 method: as_na(x, ...) ## S3 method for class 'data.frame' as_na(x, ...) ## S3 method for class 'list' as_na(x, ...)
as_na(x, ...) as.na(x, ...) ## Default S3 method: as_na(x, ...) ## S3 method for class 'data.frame' as_na(x, ...) ## S3 method for class 'list' as_na(x, ...)
x |
The object at stake. |
... |
Additional arguments (unused). |
An object of the same class as x
;
the attributes of x
are passed unchanged to the result.
x <- c("a", "b", "c") as_na(x) class(as_na(x)) # still a character x <- factor(LETTERS) as_na(x) # levels are kept class(as_na(x)) # still a factor x <- data.frame(x = 1:3, y = 2:4) as_na(x) dim(as_na(x)) x <- matrix(1:6, 2, 3) attr(x, "today") <- Sys.Date() as_na(x) # attributes are kept
x <- c("a", "b", "c") as_na(x) class(as_na(x)) # still a character x <- factor(LETTERS) as_na(x) # levels are kept class(as_na(x)) # still a factor x <- data.frame(x = 1:3, y = 2:4) as_na(x) dim(as_na(x)) x <- matrix(1:6, 2, 3) attr(x, "today") <- Sys.Date() as_na(x) # attributes are kept
bazar provides a collection of miscellaneous functions for
copying objects to the clipboard (Copy
);
loading or showing packages (library_with_dep
,
require_with_dep
, sessionPackages
);
creating or testing for named lists (nlist
,
as_nlist
, is_nlist
),
formulas (is_formula
), empty objects
(as_empty
, is_empty
),
whole numbers (as_wholenumber
,
is_wholenumber
);
testing for equality (almost_equal
,
almost_zero
);
The function concat
concatenates character vectors all together.
concat0(.)
is a wrapper for concat(., sep = "")
.
concat_(.)
is a wrapper for concat(., sep = "_")
.
concat(..., sep = " ", na.rm = TRUE) concat0(..., na.rm = TRUE) concat_(..., na.rm = TRUE)
concat(..., sep = " ", na.rm = TRUE) concat0(..., na.rm = TRUE) concat_(..., na.rm = TRUE)
... |
One or more objects, to be converted to character vectors and concatenated. |
sep |
character. The character to use to separate the result. |
na.rm |
logical. If |
Always a character value (vector of length 1
).
v <- c("Florence", "Julie", "Angela") concat0(v) concat_(v) concat(v, sep = "^^") concat0(c("a", "b"), c(1, NA, 3), NA) concat(c(NA, NA)) concat(c(NA, NA), na.rm = FALSE) # usually not desirable
v <- c("Florence", "Julie", "Angela") concat0(v) concat_(v) concat(v, sep = "^^") concat0(c("a", "b"), c(1, NA, 3), NA) concat(c(NA, NA)) concat(c(NA, NA), na.rm = FALSE) # usually not desirable
The function Copy
can typically be used
to copy data from a data frame, in order to paste
it somewhere else (in Excel for instance).
Copy(x, size = 128L, quote = TRUE, sep = "\t", na = "", dec = ".", ...)
Copy(x, size = 128L, quote = TRUE, sep = "\t", na = "", dec = ".", ...)
x |
An object. |
size |
integer. Number of kilobytes.
Increase this value if the object |
quote |
See the eponymous argument in |
sep |
character. The field separator string. |
na |
character. The string to use for missing values. |
dec |
character. The string to use for decimal points in numeric or complex columns. |
... |
Additional arguments to be passed to
|
The function erase
deletes all objects
that live in the calling environment.
erase(ask = TRUE)
erase(ask = TRUE)
ask |
logical. If |
use this function with care!
get_all_funs
provides all the functions exported by a given
installed package.
get_all_funs(pkg)
get_all_funs(pkg)
pkg |
character. The package of interest. (Must be installed already.) |
A character vector, the functions exported.
get_all_funs("stats")
get_all_funs("stats")
get_all_pkgs
provides all packages (belonging to a given list of
packages) exported by a given function.
get_all_pkgs(fun, packages = NULL)
get_all_pkgs(fun, packages = NULL)
fun |
function or character. The function of interest. |
packages |
The packages to look into. If |
A character vector, the packages.
## Not run: get_all_pkgs("as.fun") get_all_pkgs(as.fun) get_all_pkgs("stats::median") ## End(Not run)
## Not run: get_all_pkgs("as.fun") get_all_pkgs(as.fun) get_all_pkgs("stats::median") ## End(Not run)
The function get_vars
extracts variable names from a formula.
get_vars(formula, data = NULL, intersection = TRUE)
get_vars(formula, data = NULL, intersection = TRUE)
formula |
a formula. |
data |
data.frame or matrix. If not |
intersection |
logical. If |
a character vector, the variables found.
get_vars(y ~ x1 + x2 - x1) get_vars(y ~ . - x1, data = data.frame(y = 1, x1 = 2, x2 = 3)) get_vars(y + z ~ x1 + x2 - x1 | x3) get_vars(y ~ x1 + I(log(x2))) get_vars(y ~ x1*x2) get_vars(y ~ x1:x2) get_vars(~ x1 + x2)
get_vars(y ~ x1 + x2 - x1) get_vars(y ~ . - x1, data = data.frame(y = 1, x1 = 2, x2 = 3)) get_vars(y + z ~ x1 + x2 - x1 | x3) get_vars(y ~ x1 + I(log(x2))) get_vars(y ~ x1*x2) get_vars(y ~ x1:x2) get_vars(~ x1 + x2)
These methods test if an object x
is empty.
is_empty(x) is.empty(x) ## Default S3 method: is_empty(x) ## S3 method for class 'data.frame' is_empty(x)
is_empty(x) is.empty(x) ## Default S3 method: is_empty(x) ## S3 method for class 'data.frame' is_empty(x)
x |
An object to be tested. |
TRUE
if x
is empty, FALSE
otherwise.
as_empty
in this package.
is_empty(4) is_empty(c()) is_empty(new.env()) is_empty(character(0)) is_empty(list()) is_empty(integer(0)) is_empty(data.frame())
is_empty(4) is_empty(c()) is_empty(new.env()) is_empty(character(0)) is_empty(list()) is_empty(integer(0)) is_empty(data.frame())
The function is_formula
tests if the object
x
is a formula.
is_formula(x) is.formula(x)
is_formula(x) is.formula(x)
x |
An object. |
A logical, TRUE
if x
is a formula.
is_formula("this is a formula") is_formula(f <- formula("y ~ x")) is_formula(update(f, ~ . -1))
is_formula("this is a formula") is_formula(f <- formula("y ~ x")) is_formula(update(f, ~ . -1))
The function is_wholenumber
tests if values of
the numeric vector x
are all whole numbers (up
to a tolerance).
The function as_wholenumber
is a synonym for
as.integer
.
is_wholenumber(x, tolerance = sqrt(.Machine$double.eps)) is.wholenumber(x, tolerance = sqrt(.Machine$double.eps)) as_wholenumber(x, ...) as.wholenumber(x, ...)
is_wholenumber(x, tolerance = sqrt(.Machine$double.eps)) is.wholenumber(x, tolerance = sqrt(.Machine$double.eps)) as_wholenumber(x, ...) as.wholenumber(x, ...)
x |
a vector to be tested. |
tolerance |
numeric. Differences smaller than tolerance are considered as equal.
The default value is close to |
... |
Additional arguments passed to or from other methods. |
A logical, TRUE
if all values of x
are (finite) whole numbers.
If x
contains NA
or NaN
, then NA
is returned.
x = c(1L, 10L) is.integer(x) is_wholenumber(x) x = c(1, 10) is.integer(x) is_wholenumber(x) # here is the difference with 'is.integer' is_wholenumber(1+10^(-7)) is_wholenumber(1+10^(-8))
x = c(1L, 10L) is.integer(x) is_wholenumber(x) x = c(1, 10) is.integer(x) is_wholenumber(x) # here is the difference with 'is.integer' is_wholenumber(1+10^(-7)) is_wholenumber(1+10^(-8))
isNA
tests if an object x
is identical to one of NA
,
NA_character_
, NA_complex_
, NA_integer_
,
NA_real_
, or NaN
.
isNA(x)
isNA(x)
x |
An R object. |
TRUE
or FALSE
.
library_with_dep
and require_with_dep
behave
respectively like library
and
require
, but also load and attach
dependent packages (typically packages listed in the Imports
field of the DESCRIPTION
file).
library_with_dep(package, help, pos = 2, lib.loc = NULL, character.only = FALSE, logical.return = FALSE, warn.conflicts = TRUE, quietly = FALSE, verbose = getOption("verbose"), which = "Imports", recursive = FALSE, reverse = FALSE) require_with_dep(package, lib.loc = NULL, quietly = FALSE, warn.conflicts = TRUE, character.only = FALSE, which = "Imports", recursive = FALSE, reverse = FALSE, verbose = getOption("verbose"))
library_with_dep(package, help, pos = 2, lib.loc = NULL, character.only = FALSE, logical.return = FALSE, warn.conflicts = TRUE, quietly = FALSE, verbose = getOption("verbose"), which = "Imports", recursive = FALSE, reverse = FALSE) require_with_dep(package, lib.loc = NULL, quietly = FALSE, warn.conflicts = TRUE, character.only = FALSE, which = "Imports", recursive = FALSE, reverse = FALSE, verbose = getOption("verbose"))
package |
the name of a package, given as a name or literal character string,
or a character string, depending on whether character.only is
|
help |
the name of a package, given as a name or literal character string,
or a character string, depending on whether character.only is
|
pos |
the position on the search list at which to attach
the loaded namespace.
Can also be the name of a position on the current
search list as given by |
lib.loc |
character. A vector describing the location of R
library trees to search through, or |
character.only |
logical. Indicates whether |
logical.return |
logical. If it is |
warn.conflicts |
logical. If |
quietly |
logical. If |
verbose |
logical. If |
which |
character. A vector listing the types of dependencies,
a subset of |
recursive |
logical. Should (reverse) dependencies of (reverse) dependencies (and so on) be included? |
reverse |
logical. If |
library
and
require
from package base;
package_dependencies
from tools;
installed.packages
from utils.
The function mgsub
is a ‘multiple’ version of gsub
.
mgsub(pattern, replacement, x, ...)
mgsub(pattern, replacement, x, ...)
pattern |
character vector containing regular expressions to be matched in the given character vector. |
replacement |
a replacement vector of the same length as |
x |
vector or NULL: the values to be matched against. |
... |
additional parameters to be passed to |
A character vector of the same length as x
.
Theodore Lytras on StackOverflow, see http://stackoverflow.com/a/15254254/3902976
gsub
from package base.
mgsub(c("aa", "AA"), c("bb", "BB"), c("XXaaccAACC", "YYaaccAACC", "ZZaaccAACC"))
mgsub(c("aa", "AA"), c("bb", "BB"), c("XXaaccAACC", "YYaaccAACC", "ZZaaccAACC"))
Functions to construct, coerce and check for named lists.
nlist(...) as_nlist(x, ...) is_nlist(x) as.nlist(x, ...) is.nlist(x)
nlist(...) as_nlist(x, ...) is_nlist(x) as.nlist(x, ...) is.nlist(x)
... |
Named objects. |
x |
Object to be coerced or tested. |
A named list.
x <- nlist(x = 2, y = c("a", "b")) is_nlist(x)
x <- nlist(x = 2, y = c("a", "b")) is_nlist(x)
This function divides x
by the result of fun(x)
.
normalize(x, fun = "max", na.rm = TRUE, ...)
normalize(x, fun = "max", na.rm = TRUE, ...)
x |
numeric. A vector. |
fun |
character or function. Should own an |
na.rm |
Should missing values be removed in the calculation of |
... |
Additional arguments to be passed to |
A numeric vector of the same length as x
.
x <- rnorm(10) normalize(x)
x <- rnorm(10) normalize(x)
The pause
function stops momentarily the
execution of a program.
Pressing <Enter> continues the execution; typing 'stop'
(without quotation marks) ends the program.
pause(duration = Inf)
pause(duration = Inf)
duration |
numeric or infinite.
If |
Compute the lengths and values of runs of
almost_equal
values in a vector.
rle2(x, tolerance = sqrt(.Machine$double.eps))
rle2(x, tolerance = sqrt(.Machine$double.eps))
x |
numeric vector. |
tolerance |
numeric. Differences smaller than tolerance are considered as equal.
The default value is close to |
An object of class "rle"
which is a list with components:
lengths
: an integer vector containing the length of each run.
values
: a vector of the same length as lengths with the
corresponding values.
almost_equal
in this package;
rle
in package base.
Windowed / rolling operations on a vector, with a custom function fun
provided as input.
rollfun(x, k, fun = "mean", ..., .idx = NULL) make_idx(k, n)
rollfun(x, k, fun = "mean", ..., .idx = NULL) make_idx(k, n)
x |
A vector. |
k |
integer. Width of moving window; must be an integer between one and
|
fun |
character or function. The function to be applied on moving subvectors of
|
... |
Additional arguments to be passed to |
.idx |
integer. A vector of indices that can be precalculated with the function
|
n |
integer. Length of the input vector |
Functions roll_mean
and others in package RcppRoll for a more
efficient implementation of rollfun
to specific values of fun
.
Similarly, see functions rollmean
and others in package zoo and
functions runmean
and others in package caTools.
set.seed(1) x <- sample(1:10) rollfun(x, k = 3) rollfun(x, k = 3, fun = max)
set.seed(1) x <- sample(1:10) rollfun(x, k = 3) rollfun(x, k = 3, fun = max)
The function sessionPackages
prints the list
of packages attached to the current R session.
sessionPackages(package = NULL)
sessionPackages(package = NULL)
package |
a character vector naming installed packages,
or |
This function reuses part of the code from
sessionInfo
.
A list with the following components:
basePkgs
: a character vector of base packages which are attached.
otherPkgs
(not always present): a character vector of other attached packages.
sessionInfo
from package utils,
R.version
from package base.
sessionPackages()
sessionPackages()
If any of the expressions in ...
are not all FALSE
,
stop is called, producing an error message indicating
the first of the elements of ...
which were not false.
stopif(...)
stopif(...)
... |
Any number of (logical) R expressions,
which should evaluate to |
(NULL
if all statements in ...
are FALSE
.)
stopifnot
from package base.
## Not run: stopif(is.empty(c(2,1)), 4 < 3) # all FALSE stopif(is.empty(numeric(0))) ## End(Not run)
## Not run: stopif(is.empty(c(2,1)), 4 < 3) # all FALSE stopif(is.empty(numeric(0))) ## End(Not run)
The function sumNA
returns the sum of all the values in its arguments.
Contrarily to sum
, it returns NA
instead of 0
when the input
contains only missing values and missing values are removed.
sumNA(..., na.rm = FALSE)
sumNA(..., na.rm = FALSE)
... |
numeric or complex or logical vectors. |
na.rm |
logical. Should missing values (including |
The sum. Returns NA
if x
contains only missing values
and na.rm = TRUE
.
sum
.
x <- c(NA, NA) sum(x) sumNA(x) sum(x, na.rm = TRUE) sumNA(x, na.rm = TRUE) # here is the difference with 'sum()' sum(c()) sumNA(c())
x <- c(NA, NA) sum(x) sumNA(x) sum(x, na.rm = TRUE) sumNA(x, na.rm = TRUE) # here is the difference with 'sum()' sum(c()) sumNA(c())
top(x)
is an alias for head(x, 1L)
.
bot(x)
is an alias for tail(x, 1L)
.
top(x) bot(x)
top(x) bot(x)
x |
an object. |
An object (usually) like x
but generally smaller.
head
and head
from package
utils
The function trim
removes unnecessary whitespaces
from a character vector.
trim(x)
trim(x)
x |
character. The character vector at stake. |
A character vector of the same length as x
.
trim(c(" a b", "Hello World "))
trim(c(" a b", "Hello World "))
The unwhich
function is a kind of inverse
of the which
function.
unwhich(w, n)
unwhich(w, n)
w |
A vector of integers; morally the result of a call to |
n |
integer. The length of the result; morally the length of the
|
A logical vector of length n
.
x1 <- c(TRUE, FALSE, TRUE, TRUE) x2 <- unwhich(which(x1), length(x1)) identical(x1, x2) # TRUE w1 <- c(2, 4, 5, 1, 1) w2 <- which(unwhich(w1, 10)) identical(sort(unique(as.integer(w1))), w2) # TRUE
x1 <- c(TRUE, FALSE, TRUE, TRUE) x2 <- unwhich(which(x1), length(x1)) identical(x1, x2) # TRUE w1 <- c(2, 4, 5, 1, 1) w2 <- which(unwhich(w1, 10)) identical(sort(unique(as.integer(w1))), w2) # TRUE
The verlan
function reverses the order of
the characters in a string.
verlan(x)
verlan(x)
x |
character. A vector of strings. |
A character vector of the same length as x
.
verlan("baba") ## "abab" verlan(c("radar", "paul")) ## c("radar", "luap")
verlan("baba") ## "abab" verlan(c("radar", "paul")) ## c("radar", "luap")