Use package glm2
and mlbench
in R, I am testing with the BreastCancer
dataset. I started with 10 columns in the data frame with 479 observations.
I want to convert 9 out of 10 columns from factor to numeric and preserve the data frame but my new data frame became a data frame of 2 columns instead of the 10 ?
Here is my code
library(mlbench)
library(glm2)
data(BreastCancer)
BC = na.omit(BreastCancer)
BC = BC[, -1]
indexes = sample(1:nrow(BC), size=0.3*nrow(BC))
BCtrain = BC[-indexes,]
as.numeric.factor <- function(x) as.numeric(levels(x))[x]
BCtrain_x <- lapply(BCtrain[, -which(names(BCtrain) %in% c("Class"))], as.numeric.factor)
BCtrain_x <- as.data.frame(rbind(BCtrain_x, BCtrain$Class))