This R package provides helper functions I found useful when developing R code - perhaps you will too! The released package version can be installed via:
install.packages("oeli")
The following shows some demos. Click the headings for references on all available helpers in each category.
Distributions
The package has density and sampling functions for some distributions not included in base R, like the Dirichlet:
ddirichlet(x = c(0.2, 0.3, 0.5), concentration = 1:3)
#> [1] 4.5
rdirichlet(concentration = 1:3)
#> [1] 0.13324172 0.08290917 0.78384911
For faster computation, Rcpp implementations are also available:
Function helpers
Retrieving default arguments of a function
:
f <- function(a, b = 1, c = "", ...) { }
function_defaults(f)
#> $b
#> [1] 1
#>
#> $c
#> [1] ""
Indexing helpers
Create all possible permutations of vector elements:
permutations(LETTERS[1:3])
#> [[1]]
#> [1] "A" "B" "C"
#>
#> [[2]]
#> [1] "A" "C" "B"
#>
#> [[3]]
#> [1] "B" "A" "C"
#>
#> [[4]]
#> [1] "B" "C" "A"
#>
#> [[5]]
#> [1] "C" "A" "B"
#>
#> [[6]]
#> [1] "C" "B" "A"
Package helpers
Quickly have a basic logo for your new package:
logo <- package_logo("my_package", brackets = TRUE)
print(logo)
How to print a matrix
without filling up the entire console?
x <- matrix(rnorm(10000), ncol = 100, nrow = 100)
print_matrix(x, rowdots = 4, coldots = 4, digits = 2, label = "what a big matrix")
#> what a big matrix : 100 x 100 matrix of doubles
#> [,1] [,2] [,3] ... [,100]
#> [1,] -0.21 1.79 -0.16 ... -1.09
#> [2,] -0.58 -1.15 1.46 ... -1
#> [3,] -1.64 -0.31 -1.12 ... 0.07
#> ... ... ... ... ... ...
#> [100,] 1.06 1.83 -0.04 ... -1.33
And what about a data.frame
?
x <- data.frame(x = rnorm(1000), y = LETTERS[1:10])
print_data.frame(x, rows = 7, digits = 0)
#> x y
#> 1 0 A
#> 2 -1 B
#> 3 2 C
#> 4 0 D
#> <993 rows hidden>
#>
#> 998 -1 H
#> 999 2 I
#> 1000 2 J
Simulation helpers
Let’s simulate correlated regressor values from different marginal distributions:
labels <- c("P", "C", "N1", "N2", "U")
n <- 100
marginals <- list(
"P" = list(type = "poisson", lambda = 2),
"C" = list(type = "categorical", p = c(0.3, 0.2, 0.5)),
"N1" = list(type = "normal", mean = -1, sd = 2),
"U" = list(type = "uniform", min = -2, max = -1)
)
correlation <- matrix(
c(1, -0.3, -0.1, 0, 0.5,
-0.3, 1, 0.3, -0.5, -0.7,
-0.1, 0.3, 1, -0.3, -0.3,
0, -0.5, -0.3, 1, 0.1,
0.5, -0.7, -0.3, 0.1, 1),
nrow = 5, ncol = 5
)
data <- correlated_regressors(
labels = labels, n = n, marginals = marginals, correlation = correlation
)
head(data)
#> P C N1 N2 U
#> 1 2 3 0.28054900 -0.2664126 -1.630263
#> 2 4 2 -0.05037582 -0.6722802 -1.350827
#> 3 1 1 3.55001847 1.2912325 -1.509097
#> 4 1 3 0.41071971 -1.0315166 -1.974558
#> 5 2 3 1.04476544 0.0975537 -1.972854
#> 6 2 3 -2.19746308 -0.5906266 -1.385856
cor(data)
#> P C N1 N2 U
#> P 1.00000000 -0.2783753 -0.1117716 -0.01414418 0.54579038
#> C -0.27837534 1.0000000 0.2071735 -0.53935363 -0.72732467
#> N1 -0.11177156 0.2071735 1.0000000 -0.30000000 -0.28153377
#> N2 -0.01414418 -0.5393536 -0.3000000 1.00000000 0.09985643
#> U 0.54579038 -0.7273247 -0.2815338 0.09985643 1.00000000
Transformation helpers
The group_data.frame()
function groups a given data.frame
based on the values in a specified column:
df <- data.frame("label" = c("A", "B"), "number" = 1:10)
group_data.frame(df = df, by = "label")
#> $A
#> label number
#> 1 A 1
#> 3 A 3
#> 5 A 5
#> 7 A 7
#> 9 A 9
#>
#> $B
#> label number
#> 2 B 2
#> 4 B 4
#> 6 B 6
#> 8 B 8
#> 10 B 10
Validation helpers
Is my matrix a proper transition probability matrix?
matrix <- diag(4)
matrix[1, 2] <- 1
check_transition_probability_matrix(matrix)
#> [1] "Must have row sums equal to 1"