Learn R Programming

cheese (version 0.0.3)

typly: Apply function(s) to elements conforming to specified type(s)

Description

Evaluates a function or a list of function on all elements of list or data.frame that inherit at least one of the allowable types specified by the user. An option is available to evaluate the function(s) on all elements that do not match.

Usage

typly(
    data,
    types,
    f,
    negated = FALSE,
    keep = FALSE,
    ...
)

Arguments

data

A data.frame or list.

types

A character vector of allowable data types identifying columns in which function(s) should be applied.

f

A function or list of functions.

negated

Should the function(s) be applied to columns that don't match any types? Defaults to FALSE.

keep

Should the non-matching columns be kept as is? Defaults to FALSE.

...

Additional arguments to be passed to f.

Value

A list with the result(s) of f for each applicable column.

Examples

Run this code
# NOT RUN {
require(tidyverse)

heart_disease %>%
    
    #Compute means and medians on numeric data
    typly(
        c("numeric", "logical"),
        list(
            mean = mean,
            median = median
        ),
        keep = TRUE,
        na.rm = TRUE
    ) %>%
    
    #Compute table
    typly(
        "factor",
        table,
        keep = TRUE
    )
# }

Run the code above in your browser using DataLab