ifsingle: Conditional element selection

View source: R/misc_funs.R

ifsingleR Documentation

Conditional element selection

Description

Tiny functions shorter, and hopefully more explicit, than ifelse.

Usage

ifsingle(x, yes, no)

ifunit(x, yes, no)

Arguments

x

A vector (ifsingle) or a numeric of length 1 (ifunit).

yes

Something of length 1. Result if the condition is fulfilled.

no

Something of length 1. Result if the condition is not fulfilled.

Details

Yes, ifunit is identical to ifelse(test == 1, yes, no). And regarding ifsingle, it is identical to ifelse(length(test) == 1, yes, no).

Why writing these functions then? Actually, I've found that they make the code more explicit, and this helps!

Value

Returns something of length 1.

Functions

  • ifunit: Conditional element selection depending on whether x is equal to unity or not.

Author(s)

Laurent Berge

Examples


# Let's create an error message when NAs are present
my_crossprod = function(mat){
 if(anyNA(mat)){
   row_na = which(rowSums(is.na(mat)) > 0)
   n_na = length(row_na)
   stop("In argument 'mat': ", n_letter(n_na), " row", plural(n_na, "s.contain"),
        " NA values (", ifelse(n_na<=3, "", "e.g. "), "row",
        enumerate_items(head(row_na, 3), "s"), ").
        Please remove ", ifunit(n_na, "it", "them"), " first.")
 }
 crossprod(mat)
}

mat = matrix(rnorm(30), 10, 3)
mat4 = mat1 = mat
mat4[c(1, 7, 13, 28)] = NA
mat1[7] = NA

# Error raised because of NA: informative (and nice) messages
try(my_crossprod(mat4))
try(my_crossprod(mat1))



dreamerr documentation built on Aug. 24, 2023, 1:08 a.m.