Skip to contents

Apply a function over selected columns.

Usage

capply(
  data,
  select,
  fun = function(x) {
 },
  ...
)

Arguments

data

A data frame.

select

A logical or integer vector representing the column to be selected. select = TRUE will select all columns.

fun

The function to be applied to each selected column.

...

Optional arguments to fun.

Value

data.frame

Details

Has some coercion limitations. e.g. you cannot convert to factor (it will result in character columns).

Examples


#----------------------------------------------------------------------------
# capply() examples
#----------------------------------------------------------------------------
library(bkmisc)

df <- data.frame(factor = factor(c("a", "b")), numeric = c(1, 2))

# convert factor column to character
capply(df, sapply(df, is.factor), as.character)
#>   factor numeric
#> 1      a       1
#> 2      b       2