add_class
,
extend_with
calculate_distance
,
project_to_segments
,
calculate_mean
expand_matrix
scale_uniform
,
scale_minmax
,
scale_quantile
inherit_default_params
check_packages
,
install_packages
random_time_string
list_as_tibble
,
tibble_as_list
,
extract_row_to_list
,
mapdf
safe_tempdir
%all_in%
,
%has_names%
,
is_single_numeric
,
is_bounded
add_class
: Add a class to an objectcalculate_distance
: Compute pairwise distances between
two matricesSee ?calculate_distance
for the list of currently
supported distances.
x <- matrix(runif(30), ncol = 10)
y <- matrix(runif(50), ncol = 10)
calculate_distance(x, y, method = "euclidean")
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1.184305 0.9571034 1.1284052 1.022205 1.5531565
#> [2,] 1.078353 1.1378581 0.7076092 1.072164 0.8846294
#> [3,] 1.335627 1.4116406 1.3526085 1.239853 1.6282805
For euclidean distances, this is similar to calculating:
project_to_segments
: Project a set of points to to set
of segmentsx <- matrix(rnorm(50, 0, .5), ncol = 2)
segfrom <- matrix(c(0, 1, 0, -1, 1, 0, -1, 0), ncol = 2, byrow = TRUE)
segto <- segfrom / 10
fit <- project_to_segments(x, segfrom, segto)
ggplot() +
geom_segment(aes(x = x[,1], xend = fit$x_proj[,1], y = x[,2], yend = fit$x_proj[,2], colour = "projection"), linetype = "dashed") +
geom_point(aes(x[,1], x[,2], colour = "point")) +
geom_segment(aes(x = segfrom[,1], xend = segto[,1], y = segfrom[,2], yend = segto[,2], colour = "segment")) +
scale_colour_brewer(palette = "Dark2") +
scale_x_continuous(name = NULL, breaks = NULL) +
scale_y_continuous(name = NULL, breaks = NULL) +
labs(colour = "Object type") +
theme_classic()
calculate_mean
: Calculate a (weighted) mean between
vectors or a list of vectors; supports the arithmetic, geometric and
harmonic meancalculate_arithmetic_mean(0.1, 0.5, 0.9)
#> [1] 0.5
calculate_geometric_mean(0.1, 0.5, 0.9)
#> [1] 0.3556893
calculate_harmonic_mean(0.1, 0.5, 0.9)
#> [1] 0.2288136
calculate_mean(.1, .5, .9, method = "harmonic")
#> [1] 0.2288136
# example with multiple vectors
calculate_arithmetic_mean(c(0.1, 0.9), c(0.2, 1))
#> [1] 0.15 0.95
# example with a list of vectors
vectors <- list(c(0.1, 0.2), c(0.4, 0.5))
calculate_geometric_mean(vectors)
#> [1] 0.2000000 0.3162278
# example of weighted means
calculate_geometric_mean(c(0.1, 10), c(0.9, 20), c(0.5, 2), weights = c(1, 2, 5))
#> [1] 0.4736057 4.3491186
expand_matrix
: Add rows and columns to a matrixx <- matrix(runif(12), ncol = 4, dimnames = list(c("a", "c", "d"), c("D", "F", "H", "I")))
expand_matrix(x, letters[1:5], LETTERS[1:10], fill = 0)
#> A B C D E F G H I J
#> a 0 0 0 0.2937302 0 0.5033395 0 0.7581031 0.5476466 0
#> b 0 0 0 0.0000000 0 0.0000000 0 0.0000000 0.0000000 0
#> c 0 0 0 0.1912601 0 0.8770575 0 0.7244989 0.7117439 0
#> d 0 0 0 0.8864509 0 0.1891936 0 0.9437248 0.3889051 0
#> e 0 0 0 0.0000000 0 0.0000000 0 0.0000000 0.0000000 0
scale_uniform
: Rescale data to have a certain center
and max rangeGenerate a matrix from a normal distribution with a large standard deviation, centered at c(5, 5).
Center the dataset at c(0, 0) with a minimum of c(-.5, -.5) and a maximum of c(.5, .5).
Check the ranges and verify that the scaling is correct.
scale_minmax
: Rescale data to a [0, 1] rangeCheck the ranges and verify that the scaling is correct.
scale_quantile
: Cut off outer quantiles and rescale to
a [0, 1] rangeCheck the ranges and verify that the scaling is correct.
apply(x_scaled3, 2, range) # each column should be [0, 1]
#> [,1] [,2]
#> [1,] 0 0
#> [2,] 1 1
qplot(x_scaled2[,1], x_scaled3[,1]) + theme_bw()
#> Warning: `qplot()` was deprecated in ggplot2 3.4.0.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
inherit_default_params
: Have one function inherit the
default parameters from other functionscheck_packages
: Easily checking whether certain
packages are installedinstall_packages
: Install packages taking into account
the remotes of anotherThis is useful for installing suggested packages with GitHub remotes.
> install_packages("SCORPIUS", package = "dynmethods", prompt = TRUE)
Following packages have to be installed: SCORPIUS
Do you want to install these packages? (y/yes/1 or n/no/2): 1
Installing SCORPIUS
...
** testing if installed package can be loaded
* DONE (SCORPIUS)
Installed SCORPIUS
[1] "SCORPIUS"
list_as_tibble
: Convert a list of lists to a tibble
whilst retaining class informationli <- list(
list(a = 1, b = log10, c = "parrot") %>% add_class("myobject"),
list(a = 2, b = sqrt, c = "quest") %>% add_class("yourobject")
)
tib <- list_as_tibble(li)
tib
#> # A tibble: 2 × 4
#> a b c .object_class
#> <dbl> <list> <chr> <list>
#> 1 1 <fn> parrot <chr [2]>
#> 2 2 <fn> quest <chr [2]>
tibble_as_list
: Convert a tibble back to a list of
lists whilst retaining class informationli <- tibble_as_list(tib)
li
#> [[1]]
#> $a
#> [1] 1
#>
#> $b
#> function (x) .Primitive("log10")
#>
#> $c
#> [1] "parrot"
#>
#> attr(,"class")
#> [1] "myobject" "list"
#>
#> [[2]]
#> $a
#> [1] 2
#>
#> $b
#> function (x) .Primitive("sqrt")
#>
#> $c
#> [1] "quest"
#>
#> attr(,"class")
#> [1] "yourobject" "list"
extract_row_to_list
: Extracts one row from a tibble and
converts it to a listmapdf
: Apply a function to each row of a data
frameThe mapdf
functions apply a function on each row of a
data frame. They are based heavily on purrr’s map
functions.
Or use an anonymous function.
tib %>% mapdf(function(row) paste0(row$b(row$a), "_", row$c))
#> [[1]]
#> [1] "0_parrot"
#>
#> [[2]]
#> [1] "1.4142135623731_quest"
Or even a formula.
tib %>% mapdf(~ .$b)
#> [[1]]
#> function (x) .Primitive("log10")
#>
#> [[2]]
#> function (x) .Primitive("sqrt")
There are many more variations available. See ?mapdf
for
more info.
%all_in%
: Check whether a vector are all elements of
another vectorlibrary(assertthat)
assert_that(c(1, 2) %all_in% c(0, 1, 2, 3, 4))
#> [1] TRUE
assert_that("a" %all_in% letters)
#> [1] TRUE
assert_that("A" %all_in% letters)
#> Error: "A" is missing 1 element from letters: "A"
assert_that(1:10 %all_in% letters)
#> Error: 1:10 is missing 10 elements from letters: 1L, 2L, 3L, ...
%has_names%
: Check whether an object has certain
namesis_single_numeric
: Check whether a value is a single
numericassert_that(is_single_numeric(1))
#> [1] TRUE
assert_that(is_single_numeric(Inf))
#> [1] TRUE
assert_that(is_single_numeric(1.6))
#> [1] TRUE
assert_that(is_single_numeric(NA))
#> Error: NA is not a single numeric value
assert_that(is_single_numeric(1:6))
#> Error: 1:6 is not a single numeric value
assert_that(is_single_numeric("pie"))
#> Error: "pie" is not a single numeric value
is_bounded
: Check whether a value within a certain
intervalassert_that(is_bounded(10))
#> [1] TRUE
assert_that(is_bounded(10:30))
#> [1] TRUE
assert_that(is_bounded(Inf))
#> Error: Inf is not bounded by (-Inf,Inf)
assert_that(is_bounded(10, lower_bound = 20))
#> Error: 10 is not bounded by (20,Inf)
assert_that(is_bounded(
10,
lower_bound = 20,
lower_closed = TRUE,
upper_bound = 30,
upper_closed = FALSE
))
#> Error: 10 is not bounded by [20,30)