Replacing ? in the dataset in R


#1

I have a data set which has '?" in place of missing values… how can i replace them as a different value…tried the following code after converting the column in to character type… It was initially read as a factor…

train$Workclass[train$Workclass==’?’] = “Unknown_Work”

but its not working…

Thanks
Ashis


#2

Check if Workclass is a factor variable. If it is, and depending on what you want to do, you could either convert it to character and assign the string, or convert the string to factor and assign it to the elements.


#3

Try adding ‘?’ to the factors as follows:

levels(train$Workclass) = c(levels(train$Workclass), 'Unknown_Work)
and then add the above statement.

Or you could rename the level ‘?’ to Unknown_Work which would be a better option.

Let me know if it works.