I'm trying to turn some of the variables in my data.frame
into factor
while preserving the data.frame structure. I follow the suggestions HERE but I don't get my desired output, any fix?
d <- data.frame(a = c(1,2, 3, 5), b = c(2,3, 4, 2), e = c(3,4,5,1), f = rep(c("long", "short"), 2))
factor.name <- names(d)[-4]
d[] <- lapply(seq_along(factor.name), function(i) as.factor(d[factor.name[i]]))