How can I use as.numeric with data.table?

Hi!

I have been trying to convert a character column to a numeric column in my data.table, but I'm failing. It is too slow and stuck after a long time.

The regular way has been faster than the data.table way. Is it right?!

# Regular way
base_placebo$municipio <- as.numeric(base_placebo$municipio) 

# Data table way
base_placebo[, lapply(municipio, as.numeric),]

What I am missing?

Hard to tell, if anything. can you supply a minimal example with sample data?

1 Like

its simpler

library(data.table)


(adt <- as.data.table(data.frame(x=as.character(1:5))))

adt[,x:=as.numeric(x)]

str(adt)
1 Like

Thanks! Worked very well!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.