kiwi
kiwi

Reputation: 585

How to create an R function which applies function to more than one column in a dataset?

Here is an example dataset:

library(tidyverse)
library(lubridate)

db_country <- tibble(country = c("Argentina", "Australia", "Austria"),
                     region = c("Americas", "Asia", "Europe"),
                     start_date = as.numeric(18487, 18487, 18487),
                     end_date = as.numeric(18500, 18500, 18500))

# A tibble: 3 x 4
  country   region   start_date end_date
  <chr>     <chr>         <dbl>    <dbl>
1 Argentina Americas      18487    18500
2 Australia Asia          18487    18500
3 Austria   Europe        18487    18500

I am trying to make a function that converts all values in a column into a date format. This is what I have so far:

mydate <- function(dataset, column) {
  dataset %>% mutate({{column}} := as_date({{column}}))

I want to be able to input multiple column names for the "column" argument that I made. Rather than using my mydate() function twice for start_date and end_date, I want to be able to write something like this and apply the function to more than one column using one line of code: (a bit like the select() function)

mydate(db_country, start_date, end_date)

How can I edit my function to do this?

Any help is much appreciated :)

Upvotes: 1

Views: 46

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174348

You can do:

mydate <- function(dataset, ...) 
{
  mutate(dataset, across(as.character(ensyms(...)), as_date))
}

Which allows the following pipe-friendly syntax:

db_country %>% mydate(start_date, end_date)

#> # A tibble: 3 x 4
#>   country   region   start_date end_date  
#>   <chr>     <chr>    <date>     <date>    
#> 1 Argentina Americas 2020-08-13 2020-08-26
#> 2 Australia Asia     2020-08-13 2020-08-26
#> 3 Austria   Europe   2020-08-13 2020-08-26

Upvotes: 3

Related Questions