Replace NA in R language

Question:

I would like to replace NA ( missing ) with a word. I have the following data:

structure(list(NOME = c("ABC", "ADD", 
"AFF", "DDD", "RTF", "DRGG"
), TIPO = c("INTERNACAO", "", "CONSULTA", "EXAME", "", "EXAME"
), VALOR = c(10L, 20L, 30L, 40L, 50L, 60L)), class = "data.frame", row.names = c(NA, 
-6L))

#NOME        TIPO  VALOR
#ABC   INTERNACAO     10
#ADD                  20
#AFF     CONSULTA     30
#DDD        EXAME     40
#RTF                  50
#DRGG       EXAME     60

How to replace NA by the word TESTE ?

Answer:

Assuming your data is in a data frame called dat , and the column you want to replace the NA is called TIPO :

dat$TIPO[which(is.na(dat$TIPO))] <- "TESTE"

According to your data, I don't see NA in the TIPO column but empty elements. In this case, instead of NA , you use " " .

dat$TIPO[which(dat$TIPO == " ")] <- "TESTE"
Scroll to Top