Monday 21 March 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...