E B - 7 months ago 65

R Question

Use package

`glm2`

`mlbench`

`BreastCancer`

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"
```

Source (Stackoverflow)