Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have a data frame with factor columns, now I what convert it to numeric. So I used the correlation matrix.

> str(breast)

'data.frame':   699 obs. of  10 variables:

 ....

 $ class                   : Factor w/ 2 levels "2","4": 1 1 1 1 1 2 1 1 1 1 ...

> table(breast$class)

  2   4 

458 241

> cor(breast)

Error in cor(breast) : 'x' must be numeric

How to convert the Factor column to the numeric column?

1 Answer

0 votes
by (36.8k points)

breast$class <- as.numeric(as.character(breast$class))

If you want to convert multiple columns to numeric then:

indx <- sapply(breast, is.factor)

breast[indx] <- lapply(breast[indx], function(x) as.numeric(as.character(x)))

Another option is to use stringsAsFactors=FALSE while reading the file using read.table or read.csv

The other way to change column is:

 breast[,'class'] <- as.numeric(as.character(breast[,'class']))

or

 breast <- transform(breast, class=as.numeric(as.character(breast)))

If you are a beginner and want to know more about Data Science the do check out the Data Science course 

Related questions

Browse Categories

...