df_apply: df_apply

View source: R/df_apply.R

df_applyR Documentation

df_apply

Description

Apply a function to qualifying columns in a data frame, a different function otherwise

Usage

df_apply(.data, .f, .condition = is.numeric, .else = identity, ...)

Arguments

.data

Data frame containing columns to apply functions to

.f

Function to apply to columns that fit .condition()

.condition

Condition to check to apply .f()

.else

Function to apply to columns that do NOT fit .condition()

...

Additional arguments to be passed to .f()

Value

A Data frame with the columns changed as specified.

Examples

n <- 7L
test_data <-
  dplyr::tibble(
    double     = rnorm(n, 100, 10),
    integer    = (1L:n) * (1L:n),
    character  = LETTERS[1L:n],
    factor     = factor(letters[1L:n]),
    logical    = rep(c(TRUE, FALSE), length.out = n)
  )
test_data
df_apply(test_data, round, digits = -1)
test_data |> df_apply(mean, is.numeric)
test_data |> df_apply(tolower, is.character)
test_data |> df_apply(round, is.numeric, as.factor, digits = -1)

adamddh/ds303pkg documentation built on April 14, 2022, 9:42 a.m.