Gilbert Gilbert - 1 month ago 7
R Question

Levels instead of factor as values in the dataset R

I have a data set that look as follow...

enter image description here

Happened that the last row has one of two values, either

positive
or
negative
, this column is not shown in the image. Each one of those are factors. I need to populate the data set with the levels;
1
,
2
,
3
instead of
x
,
o
,
b
, or what ever the level value might be. Also I need to keep the last column intact with no changes whatsoever. How can I accomplish this in R?

Answer

Do:

mydf[ ,1:ncol(mydf)-1] <- sapply(mydf[ ,1:ncol(mydf)-1], as.integer)

Where mydf is the name of your dataframe.