Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R dplyr: select all columns after transforming selected columns

Tags:

r

dplyr

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

like image 636
Afiq Johari Avatar asked Dec 06 '25 19:12

Afiq Johari


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)
like image 50
akrun Avatar answered Dec 08 '25 11:12

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!