I want to change a couple of data.table columns from factor to character
library(data.table)
ir <- as.data.table(iris)
ir[, Species2 := Species]
I can identify which columns I need to change
facs <- which(sapply(ir, is.factor))
facs
And I can update the columns by name:
ir[, c("Species", "Species2") := lapply(.SD, as.character), .SDcols = facs]
sapply(ir, class)
Is there a way to update the columns without referencing them by name?