redeemefy
redeemefy

Reputation: 4849

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?

Upvotes: 0

Views: 73

Answers (2)

Zach
Zach

Reputation: 1153

Do:

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

Where mydf is the name of your dataframe.

Upvotes: 2

Phil
Phil

Reputation: 8127

Install the forcats package if you don't have it already.

Then repeat for as many variables as necessary the following line:

var <- fct_recode(var, 1 = "x", 2 = "o", 3 = "b")

Where var is the name of the variable in that particular column.

Upvotes: 0

Related Questions