Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Faster way of rounding and formatting column with number as character for large data table in R

I have a data table in R that I get with some columns with decimal numbers, but as a character column. The data table has 116 columns, of which 32 columns I need to round the decimal places to 3 decimal places keep the charactere format.

This data table has 20 million lines and is taking 8 hours to run the code below:

ARQUIVO_ECL_ABERTA[, grep(c("SURVIVAL"), names(ARQUIVO_ECL_ABERTA))] <- 
  data.table(sapply(ARQUIVO_ECL_ABERTA[, grep(c("SURVIVAL"),
                       names(ARQUIVO_ECL_ABERTA)), with=FALSE], 
                       function(x) sprintf('%.3f', round(as.numeric(x), 3))))

The data table is ARQUIVO_ECL_ABERTA and the columns are all columns that have SURVIVAL in the name.

Do you know a faster way to perform this task?

Thanks a lot,

Comments