In my dataset I have one column where I need to replace blanks to "No", How can I do this?
The column is a character variable and had only two values, yes and no.
data<- as.data.frame(Upper_GI_2ww)
data$`Direct to Test?` = ifelse(nchar(data$`Direct to Test?`) == "NA", "No",data$`Direct to Test? )
output:
head(data$`Direct to Test?`)
[1] "Yes" "Yes" "Yes" NA "Yes" "Yes"
Soultion:
It is working fine now, Here is what i did:
First I have converted char to factor.
I used a simple is.na function
data$`Direct to Test?`[is.na(data$`Direct to Test?`)] <-"No"
head(data$`Direct to Test?`)
Yes Yes Yes No Yes Yes
Levels: No Yes
it is working fine now.
You can use nchar to count the number of characters.
This is also valid if all you need is to replace "":