Learn R Programming

zmisc (version 0.2.3)

ddply_helper: Apply a function to each column of a data.frame

Description

Thin wrapper around lapply() that checks that the input is a table before applying the function to each column, and converts the result back to a table afterwards. If the tibble package is available and the input is a tibble, the result will be a tibble; otherwise, it will be a plain data.frame.

Usage

ddply_helper(d, fun)

Value

A data.frame or tibble with the function applied to each column.

Arguments

d

A data.frame or tibble.

fun

A function to apply to each column of d.

Examples

Run this code
df <- data.frame(
  col1 = c(1, 2, 3),
  col2 = c(4, 5, 6)
)
sum_fun <- function(x) sum(x)
result <- ddply_helper(df, sum_fun)
print(result)

Run the code above in your browser using DataLab