Remove "Nan" from a data.frame in R

Asked

Viewed 265 times

1

I have a data.frame with two columns that have some NaN and Inf and I’d like to replace them with NA. I was using this code, but it didn’t work:

library(tidyverse)

dados <- read_delim("~/Downloads/arquivo_geral.csv", 
                                        ";", escape_double = FALSE, trim_ws = TRUE)
dados <- dados[,-1]

variacaoCasos <- dados %>% 
  mutate(dif_semanal = c(rep(NA, 7), diff(casosAcumulados, 7)),
         percentual_dif = dif_semanal / lag(casosAcumulados, 7)) %>% 
  select("percentual_dif")

variacaoObitos <- dados %>% 
  mutate(dif_semanal = c(rep(NA, 7), diff(obitosAcumulados, 7)),
         percentual_dif = dif_semanal / lag(obitosAcumulados, 7)) %>% 
  select("percentual_dif")

dados <- cbind(dados, variacaoCasos, variacaoObitos)

rm(list = ls(pattern = "variacao"))

names(dados) <- c("estado", "data", "casosNovos", "casosAcumulados", "obitosNovos", 
                  "obitosAcumulados", "variacaoCasos", "VariacaoObitos")

dados$variacaoObitos <- ifelse(dados$variacaoObitos == "NaN", yes = NA, no = dados$variacaoObitos)
dados$variacaoObitos <- ifelse(dados$variacaoObitos == "Inf", yes = NA, no = dados$variacaoObitos)
dados$variacaoCasos <- ifelse(dados$variacaoCasos == "NaN", yes = NA, no = dados$variacaoCasos)
dados$variacaoCasos <- ifelse(dados$variacaoCasos == "Inf", yes = NA, no = dados$variacaoCasos)

But it’s returning the error Error in `$<-.data.frame`(`*tmp*`, variacaoObitos, value = logical(0)) : replacement has 0 rows, data has 2295.

I tried to use the function replace, however, to no avail.

Database: https://covid.saude.gov.br/

1 answer

3


The problem is in what way to test for special values like NaN and NA not with the operator ==, but with specific family functions is.

So we have the following result using ==

NA == NA
#> [1] NA
NaN == NaN
#> [1] NA

And the following result using the family is:

is.na(NA)
#> [1] TRUE
is.nan(NaN)
#> [1] TRUE

Note that, although Inf can also be tested with ==, it also has a function is.

Inf == Inf
#> [1] TRUE
is.infinite(Inf)
#> [1] TRUE
is.finite(Inf)
#> [1] FALSE

Then we would have,

dados <- data.frame(var = c(32, Inf, NaN, NA))
library(tidyverse)

dados %>% 
  mutate(sem_na = ifelse(is.na(var), 0, var),
         sem_nan = ifelse(is.nan(var), 0, var),
         sem_inf = ifelse(is.infinite(var), 0, var))

#>   var sem_na sem_nan sem_inf
#> 1  32     32      32      32
#> 2 Inf    Inf     Inf       0
#> 3 NaN      0       0     NaN
#> 4  NA      0      NA      NA

Attention because NaN is NA, but NA is not NaN.

Browser other questions tagged

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