I am writing an anonymous function to loop through a data.table and change any factor variable to a character. I tried the code below, but I receive an error. My actual data.table could have a factor variable in any order, hence why I'm using lapply. Also please share any other ways to do this, but I would like my way answered as well and thanks.
test <- sample[, lapply(.SD, function(x){
if(is.factor(x)){
as.character(x)}
})
]
sample <- data.table(A = as.factor(1:5), B = as.character(letters[1:5]), C = as.factor(10:14))