Replace NA in R language

2

Good afternoon, I would like to replace "NA" (Worthless) with a word. I have the following data:

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

How do I replace "NA" with the word "TEST"? Thankful.

    
asked by anonymous 01.12.2017 / 17:49

2 answers

2

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

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

According to your data, I do not see NA in column TIPO but empty elements. In this case, instead of NA , you use " " .

dat$TIPO[which(dat$TIPO == " ")] <- "TESTE"
    
01.12.2017 / 21:17
1

dplyr has a function called coalesce that serves just that.

In your case, you could use:

library(dplyr)
dat$TIPO <- coalesce(dat$TIPO, "TESTE")
    
04.12.2017 / 13:27