Learn R Programming

cheapr (version 1.1.0)

setdiff_: Extra utilities

Description

Extra utilities

Usage

setdiff_(x, y, dups = TRUE)

intersect_(x, y, dups = TRUE)

cut_numeric( x, breaks, labels = NULL, include.lowest = FALSE, right = TRUE, dig.lab = 3L, ordered_result = FALSE, ... )

x %in_% table

x %!in_% table

enframe_(x, name = "name", value = "value")

deframe_(x)

sample_(x, size = vector_length(x), replace = FALSE, prob = NULL)

val_insert(x, value, n = NULL, prop = NULL)

na_insert(x, n = NULL, prop = NULL)

vector_length(x)

cheapr_var(x, na.rm = TRUE)

cheapr_rev(x)

with_local_seed(expr, .seed = NULL, ...)

Value

enframe()_ converts a vector to a data frame.

deframe()_ converts a 1-2 column data frame to a vector.

intersect_() returns a vector of common values between x and y.

setdiff_() returns a vector of values in x but not y.

cut_numeric() places values of a numeric vector into buckets, defined through the breaks argument and returns a factor unless labels = FALSE, in which case an integer vector of break indices is returned.

%in_% and %!in_% both return a logical vector signifying if the values of x exist or don't exist in table respectively.

sample_() is an alternative to sample() that natively samples data frame rows through sset(). It also does not have a special case when length(x) is 1.

val_insert inserts scalar values randomly into your vector. Useful for replacing lots of data with a single value.

na_insert inserts NA values randomly into your vector. Useful for generating missing data.

vector_length behaves mostly like NROW() except for matrices in which it matches length(). cheapr_var returns the variance of a numeric vector. No coercion happens for integer vectors and so is very cheap.

cheapr_rev is a much cheaper version of rev().

with_local_seed offers no speed improvements but is extremely handy in executing random number based expressions like rnorm() without affecting the global RNG state. It allows you to run these expressions in a sort of independent 'container' and with an optional seed for that 'container' for reproducibility. The rationale for including this in 'cheapr' is that it can reduce the need to set many seed values, especially for multiple output comparisons of RNG expressions. Another way of thinking about it is that with_local_seed() is a helper that allows you to write reproducible code without side-effects, which traditionally cannot be avoided when calling set.seed() directly.

Arguments

x

A vector or data frame.

y

A vector or data frame.

dups

Should duplicates be kept? Default is TRUE.

breaks

See ?cut.

labels

See ?cut.

include.lowest

See ?cut.

right

See ?cut.

dig.lab

See ?cut.

ordered_result

See ?cut.

...

Further arguments passed onto cut or set.seed.

table

See ?collapse::fmatch

name

The column name to assign the names of a vector.

value

The column name to assign the values of a vector.

size

See ?sample.

replace

See ?sample.

prob

See ?sample.

n

Number of scalar values (or NA) to insert randomly into your vector.

prop

Proportion of scalar values (or NA) values to insert randomly into your vector.

na.rm

Should NA values be ignored in cheapr_var() Default is TRUE.

expr

Expression that will be evaluated with a local seed that is independent and has absolutely no effect on the global RNG state.

.seed

A local seed to set which is only used inside with_local_seed(). After the execution of the expression the original seed is reset.

Examples

Run this code
library(cheapr)

# Using `with_local_seed()`

# The below 2 statements are equivalent

# Statement 1
set.seed(123456789)
res <- rnorm(10)

# Statement 2
res2 <- with_local_seed(rnorm(10), .seed = 123456789)

# They are the same
identical(res, res2)

# As an example we can see that the RNG is unaffected by generating
# random uniform deviates in batches between calls to `with_local_seed()`
# and comparing to the first result

set.seed(123456789)
batch1 <- rnorm(2)

with_local_seed(runif(10))
batch2 <- rnorm(2)
with_local_seed(runif(10))
batch3 <- rnorm(1)
with_local_seed(runif(10))
batch4 <- rnorm(5)

# Combining the batches produces the same result
# therefore `with_local_seed` did not interrupt the rng sequence
identical(c(batch1, batch2, batch3, batch4), res)

# It can be useful in multiple comparisons
out1 <- with_local_seed(rnorm(5))
out2 <- with_local_seed(rnorm(5))
out3 <- with_local_seed(rnorm(5))

identical(out1, out2)
identical(out1, out3)

Run the code above in your browser using DataLab