I have a class column in my data frame which carries the value "Bad" & "Good". I want to replace these string into 0 & 1 respectively.
I tried the following:
x = c("Bad", "Good", "Bad", "Bad", "Good")
factor(x)
factor(x, c(0, 1))
but, it converts the value in the dataset to NA
factor(x, c(0, 1))
[1] <NA> <NA> <NA> <NA> <NA>`
Levels: 0 1`
as.integer(x=="Good"). Yours doesnt work asfactorlooks for levels 0/ 1 which arent there. So be explicitfactor(x, levels=c("Bad", "Good"), labels=c(0, 1))