[SOLVED] Apply a function to multiple columns in R


I have the following problem
How to apply the function dmy only to those columns of my dataframe df, which contain "Date"?

x  Cat  Date1        y   AnotherDate
1  as   31.12.2020   56  04.04.2004
2  ab   31.12.2019   78  05.09.2010
3  bs   30.09.2021   26  25.11.2012
4  bd   20.08.2021   33  31.01.2022

If I want to apply the function to all columns of dataframe, the code is the following:

apply(df, 2, dmy)

This causes an error, because dmy is not applicable to all columns.
How to transform this code, to apply dmy not to all of the columns, but only to columns, which contain the word "Date" inside their name?

Additional question: can I also use regular expressions in choosing column names inside the apply function? How to do it?


you can use the dplyr package to apply a function to several columns:


df %>%
  mutate(across(matches("Date"), dmy))

Alternatively, you can use apply but you have to select the relevant columns first. This can be done using grep:

df[, grep("Date", colnames(df))] <- apply(df[, grep("Date", colnames(df))], 2, dmy)

Answered By – Cettt

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *