简体   繁体   中英

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:

library(lubridate)
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:

library(dplyr)
library(lubridate)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM