Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert data.frame column from Factor to numeric [duplicate]

Tags:

r

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?

like image 971
birdy Avatar asked Dec 17 '14 15:12

birdy


People also ask

How do you convert a column into a numeric DataFrame?

Convert Column to int (Integer)Use pandas DataFrame. astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame. To cast the data type to 64-bit signed integer, you can use numpy. int64 , numpy.

How do I convert a factor to a numeric in a DataFrame in R?

We must first convert the factor vector to a character vector, then to a numeric vector. This ensures that the numeric vector contains the actual numeric values instead of the factor levels.

How do I convert a column to numeric in R?

To convert a column to numeric in R, use the as. numeric() function. The as. numeric() is a built-in R function that returns a numeric value or converts any value to a numeric value.

How do I convert a factor to a numeric in R?

There are two steps for converting factor to numeric: Step 1: Convert the data vector into a factor. The factor() command is used to create and modify factors in R. Step 2: The factor is converted into a numeric vector using as. numeric().


2 Answers

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))) 
like image 65
akrun Avatar answered Sep 21 '22 02:09

akrun


From ?factor:

To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).

like image 22
BrodieG Avatar answered Sep 21 '22 02:09

BrodieG