Consider a example:
library(data.table)
myDt <- data.table(a=rep(1:10), b=paste(1:10, "000",sep=","), c=paste(2:11, "000", sep=",") )
myDt
Now I want to convert the character columns into numeric. Basically I want to remove the commas. I have already figured a way.
charToNumCols <- c("b", "c")
numColsDt <- myDt[, lapply(.SD, function(x) as.numeric(gsub(",", "", x)) ), .SDcols=charToNumCols]
newDt <- data.table(myDt[, .SD, .SDcols=setdiff(names(myDt), charToNumCols)], numColsDt)
Now I want a solution in which I dont have to create an intermediate data.table. Originally for this I wrote the following R code
myDt <- myDt[, charToNumCols:=lapply(.SD, function(x) as.numeric(gsub(",", "", x)) ), .SDcols=charToNumCols]
But this does not do the required operation.
Can anyone answer what should be the most efficient coding for the above, considering data.table in R?