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

巧了我就是萌 提交于 2020-01-05 06:52:33

问题


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

回答1:


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"


来源:https://stackoverflow.com/questions/40325026/for-glm2-package-trying-to-convert-factor-to-numeric-and-preserving-it-as-a-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!