R dplyr: select all columns after transforming selected columns

Viewed 265

I have a tibble and want to only do some mutation on selected columns. In the case below, all columns which have the word 'date' will be transformed by a function (as.Date()).

After I have performed some transformations on the selected columns, I want to get back all the columns from my tibble df.

Is there a way to do so?

df %>% select(contains('date')) %>% mutate_all(as.Date) %>% select(all)

Thanks

1 Answers

We can use mutate_at instead of select and then mutate_all. This would select only the columns of interest and modify those while keeping the others columns as such

library(dplyr)
df %>%
   mutate_at(vars(contains('date')), as.Date)
Related