E B E B - 1 month ago 8
R Question

For glm2 package trying to convert factor to numeric and preserving it as a dataframe

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))

Answer

Replace your final line

BCtrain_x <- as.data.frame(rbind(BCtrain_x,BCtrain$Class))

with

BCtrain_x <- cbind.data.frame(BCtrain_x, BCtrain["Class"])

Then you will be golden.

dim(BCtrain_x)
# [1] 419 10

unlist(lapply(BCtrain_x, class))
#   Cl.thickness       Cell.size      Cell.shape   Marg.adhesion    Epith.c.size 
#      "numeric"       "numeric"       "numeric"       "numeric"       "numeric" 
#    Bare.nuclei     Bl.cromatin Normal.nucleoli         Mitoses           Class 
#      "numeric"       "numeric"       "numeric"       "numeric"        "factor"
Comments