Skip to contents

Apply a function on each column of a data frame given a condition

Usage

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

Arguments

.data

A data frame

.f

A function to apply on the data frame

.condition

Default value "is.numeric", but can be another data type

.else

Default value "identity" (skips over a column)

...

Any arguments from .f to pass

Value

A tibble

Examples

n <- 7L
TestData <-
    tibble::tibble( double = rnorm(n, 100, 10),
     x = 123400 / 10^(1L:n),
     date = sample(seq(as.Date('1999/01/01'), as.Date('2022/01/01'), by="day"), n),
     integer = (1L:n) * (1L:n),    # ^2 would return a double!!
     character = LETTERS[1L:n],
     factor = factor(letters[1L:n]),
     logical = rep(c(TRUE, FALSE), length.out = n) )
     
df_apply(TestData, round, is.numeric, digits =4)
#> # A tibble: 7 × 7
#>   double          x date       integer character factor logical
#>    <dbl>      <dbl> <date>       <dbl> <chr>     <fct>  <lgl>  
#> 1   90.0 12340      2017-12-06       1 A         a      TRUE   
#> 2  104.   1234      2016-05-27       4 B         b      FALSE  
#> 3  106.    123.     2020-02-16       9 C         c      TRUE   
#> 4  107.     12.3    2006-03-25      16 D         d      FALSE  
#> 5  120.      1.23   2003-08-20      25 E         e      TRUE   
#> 6   89.0     0.123  2015-03-05      36 F         f      FALSE  
#> 7   78.2     0.0123 2021-08-06      49 G         g      TRUE   
df_apply(TestData, mean, is.numeric)
#> # A tibble: 7 × 7
#>   double     x date       integer character factor logical
#>    <dbl> <dbl> <date>       <dbl> <chr>     <fct>  <lgl>  
#> 1   99.0 1959. 2017-12-06      20 A         a      TRUE   
#> 2   99.0 1959. 2016-05-27      20 B         b      FALSE  
#> 3   99.0 1959. 2020-02-16      20 C         c      TRUE   
#> 4   99.0 1959. 2006-03-25      20 D         d      FALSE  
#> 5   99.0 1959. 2003-08-20      20 E         e      TRUE   
#> 6   99.0 1959. 2015-03-05      20 F         f      FALSE  
#> 7   99.0 1959. 2021-08-06      20 G         g      TRUE   
df_apply(TestData, lubridate::year, lubridate::is.Date)
#> # A tibble: 7 × 7
#>   double          x  date integer character factor logical
#>    <dbl>      <dbl> <dbl>   <int> <chr>     <fct>  <lgl>  
#> 1   90.0 12340       2017       1 A         a      TRUE   
#> 2  104.   1234       2016       4 B         b      FALSE  
#> 3  106.    123.      2020       9 C         c      TRUE   
#> 4  107.     12.3     2006      16 D         d      FALSE  
#> 5  120.      1.23    2003      25 E         e      TRUE   
#> 6   89.0     0.123   2015      36 F         f      FALSE  
#> 7   78.2     0.0123  2021      49 G         g      TRUE   
df_apply(TestData, toupper, is.character)
#> # A tibble: 7 × 7
#>   double          x date       integer character factor logical
#>    <dbl>      <dbl> <date>       <int> <chr>     <fct>  <lgl>  
#> 1   90.0 12340      2017-12-06       1 A         a      TRUE   
#> 2  104.   1234      2016-05-27       4 B         b      FALSE  
#> 3  106.    123.     2020-02-16       9 C         c      TRUE   
#> 4  107.     12.3    2006-03-25      16 D         d      FALSE  
#> 5  120.      1.23   2003-08-20      25 E         e      TRUE   
#> 6   89.0     0.123  2015-03-05      36 F         f      FALSE  
#> 7   78.2     0.0123 2021-08-06      49 G         g      TRUE