Subset and maintain NA values

Asked

Viewed 204 times

3

I’m sure there’s a way to subset and keep the NA values in the R. However, when I apply this function it is also disappearing from my base the NA values. I’m just using the formula below:

dados1<-data.frame(subset(dados,V2!="CNPJ"))

So you’re missing the valuables from the V2 that’s in NA. What’s the best way to make this subset?

2 answers

3


The way I found out was to put the is.na in the programming, that is to say:

dados1<-data.frame(subset(dados,V2!="CNPJ"|is.na(V2)))
  • 1

    you don’t need to call the function data.frame again in this case. Enough subset(dados,V2!="CNPJ"|is.na(V2))

  • Beauty, Daniel.

1

One option (maybe uglier) is to use the subsetting that the operator [ provides:

dados[dados$V2 != "CNPJ",]

This operation keeps the lines containing NA.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.