Learn R Programming

Utilities for developing R code

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.2832878 0.5086812 0.2080309

For faster computation, Rcpp implementations are also available:

microbenchmark::microbenchmark(
  "R"    = rmvnorm(mean = c(0, 0, 0), Sigma = diag(3)),
  "Rcpp" = rmvnorm_cpp(mean = c(0, 0, 0), Sigma = diag(3))
)
#> Unit: microseconds
#>  expr   min     lq    mean median     uq    max neval
#>     R 275.3 295.65 345.847  310.1 332.45 2386.5   100
#>  Rcpp   2.7   3.20   6.245    4.8   5.40  164.2   100

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.79 -0.43 -0.87 ...  -2.12
#> [2,]   -1.11  1.98 -2.42 ...   1.65
#> [3,]    1.66  1.76  0.25 ...  -2.97
#> ...      ...   ...   ... ...    ...
#> [100,]  0.44  0.28  0.53 ...  -1.75

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     0 C
#> 4     0 D
#> <993 rows hidden>
#>          
#> 998   0 H
#> 999   0 I
#> 1000  0 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 1 2 -3.619643  1.24813328 -1.782100
#> 2 1 3 -4.117207  0.19133009 -1.585383
#> 3 2 1  2.146791 -0.08796485 -1.290140
#> 4 2 3 -3.501855  0.60817726 -1.688658
#> 5 1 3  2.707852 -2.17507050 -1.912338
#> 6 2 1 -2.222701  2.28324260 -1.646795
cor(data)
#>              P          C          N1          N2           U
#> P   1.00000000 -0.3164384 -0.08426915 -0.03743832  0.54776279
#> C  -0.31643843  1.0000000  0.19326415 -0.50596805 -0.75090001
#> N1 -0.08426915  0.1932641  1.00000000 -0.30000000 -0.26643345
#> N2 -0.03743832 -0.5059680 -0.30000000  1.00000000  0.09397231
#> U   0.54776279 -0.7509000 -0.26643345  0.09397231  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"

Copy Link

Version

Install

install.packages('oeli')

Monthly Downloads

855

Version

0.7.4

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Lennart Oelschläger

Last Published

June 27th, 2025

Functions in oeli (0.7.4)

dtnorm_cpp

Truncated normal distribution
Storage

Storage R6 Object
delete_columns_data.frame

Deleting data.frame columns
diff_cov

Difference and un-difference covariance matrix
dwishart_cpp

Wishart distribution
function_defaults

Get default function arguments
match_numerics

Best-possible match of two numeric vectors
check_correlation_matrix

Check correlation matrix
match_arg

Argument matching
function_body

Extract function body
occurrence_info

Provide information about occurrences
find_namespace_calls

Namespace calls
function_arguments

Get function arguments
print_matrix

Print (abbreviated) matrix
input_check_response

Standardized response to input check
simulate_markov_chain

Simulate Markov chain
merge_lists

Merge named lists
ddirichlet_cpp

Dirichlet distribution
cov_to_chol

Cholesky root of covariance matrix
split_vector_at

Split a vector at positions
quiet

Silence R code
variable_name

Determine variable name
user_confirm

User confirmation
dmvnorm_cpp

Multivariate normal distribution
do.call_timed

Measure computation time
check_numeric_vector

Check numeric vector
insert_matrix_column

Insert column in matrix
matrix_diagonal_indices

Get indices of matrix diagonal
check_missing

Check missing formal argument
subsets

Generate vector subsets
package_logo

Creating a basic logo for an R package
insert_vector_entry

Insert entry in vector
matrix_indices

Get matrix indices
oeli-package

oeli: Some Utilities for Developing Data Science Software
sample_covariance_matrix

Sample covariance matrix
map_indices

Map indices
system_information

General system level information
sample_transition_probability_matrix

Sample transition probability matrices
try_silent

Try an expression silently
timed

Interrupt long evaluations
stationary_distribution

Stationary distribution
unexpected_error

Handling of an unexpected error
correlated_regressors

Simulate correlated regressor values
group_data.frame

Grouping of a data.frame
permutations

Build permutations
chunk_vector

Split a vector into chunks
identical_structure

Check if two objects have identical structure
print_data.frame

Print (abbreviated) data.frame
vector_occurrence

Find the positions of first or last occurrence of unique vector elements
sample_correlation_matrix

Sample correlation matrix
round_data.frame

Round numeric columns of a data.frame
Dictionary

Dictionary R6 Object
check_list_of_lists

Check list of lists
Simulator

Simulator R6 Object
check_covariance_matrix

Check covariance matrix
check_transition_probability_matrix

Check transition probability matrix
check_probability_vector

Check probability vector