Tuesday 14 August 2018

r - How to convert data.frame column from Factor to numeric




I have a data.frame whose class column is Factor. I'd like to convert it to numeric so that I can use 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 can I convert a Factor column to a numeric column?


Answer



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



If you have many columns to convert to numeric



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



Just in case, other options to create/change columns




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


or



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

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...