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?
Best Solution
If you have many columns to convert to
numeric
Another option is to use
stringsAsFactors=FALSE
while reading the file usingread.table
orread.csv
Just in case, other options to create/change columns
or