Unable to fill missing value

r

#1

I just started working on this problem. I am trying to impute missing value for “Married” column. As this has only 3 missing values I tried to impute them as “No”. I used below code to impute but it is not working. Tried with different options but I am not getting any error but the missing value is still there.

loan_dat$Married[which(is.na(loan_dat$Married))] <- "No"
loan_dat$Married <- ifelse(is.na(loan_dat$Married), "No", loan_dat$Married)

Married column is categorical and not factor in my dataframe.
Any help please.


#2

@yaswanth912

Try as follow:
loan_dat$Married[is.na(loan_dat$Married)] <- "No"

I hope it works.


#3

No this also didn’t work.


#4

can you please show some line of data where NA comes so I will try on my system and let you know?


#5


Please check above lines.


#6

Ok I will try and let you know


#7

Hi @yaswanth912,

Try the following code:

loan_dat$Married[loan_dat$Married == ""] = "No"

Let us know if it works.


#8

Hey @pjoshi15, this worked. Wondering what was the difference between my code and the one you provided?
But I have changed the inverted commas from the one provided by you.

loan_dat$Married[loan_dat$Married == ""] <- "No" (this worked)


#9

Ok There is no ‘NA’ it was white space thatsy


#10

Exactly :slight_smile:


#11

I have used Pandas and used the following code for Data Munging.

data.Self_Employed = data.Self_Employed.fillna('No') and it works