Changing the class of a column using dplyr tools
03:59 01 Oct 2026

I probably think a bit to complicated, but I would like to change the class of a data.frame column, ideally with {dplyr} functions for easy selection of columns.

df <- data.frame(id=1:3, Intensity=rnorm(3) * 1E8)
class(df$Intensity) <- c(class(df$Intensity), 'scientific')

I would add the scientific class to specific columns, because I use {openxlsx2} to create an excel file and I want automatic scientific formatting for specific columns. {openxlsx2} can read those classes and set the formatting accordingly.

My test with df %>% mutate_at(vars(starts_with('Intensity')), .funs= ~ class(c(class(.), 'scientific'))) did not work, because it is overwriting the values with the class definition.

Is there a way to use the {dplyr} functions for this job?

r dplyr