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?
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.
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.
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.
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().
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)))
From ?factor
:
To transform a factor f to approximately its original numeric values,
as.numeric(levels(f))[f]
is recommended and slightly more efficient thanas.numeric(as.character(f))
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With