I am passing column names as strings into a function and wish to change the class of the corresponding column. Currently, I reference the column of the data.table using get(varName).
I have a data.table with a factor column that I wish to convert to character. Sample data:
dt <- data.table(factor(c("b","c")),foo=c(4,2))
sapply(dt, class)
Simplified attempt:
fo2 <- function(data, change){
data[,get(change):=as.character(get(change))]
return(data)
}
fo2(data=dt, change="V1")
Error in get(change) : object 'V1' not found
Thanks for any help understanding.