Search code examples
rmissing-datachi-squared

Nanair package not having expected results


Recently, I've been making chi squared tests with some variables from my dataset. The problem is that some variables have missing values listed as -9 instead of NA and I tried using a few tactics to remedy this:

Oak %>% replace_with_na_all(condition = ~.x == -9)

oakland_analysis_final %>%
   replace_with_na_all(condition = ~.x %in% common_na_numbers)

na_strings <-c("-9")

Oak %>%
   replace_with_na_all(condition = ~.x %in% na_strings)

to replace -9 with NA. After using a table() command, I confirmed that -9 was still listed. There were no apparent errors.


Solution

  • How about this?

        Oak <- data.frame(
        id = c(1,   1,  1,  1,  1,  1,  1,  1,  1,  1),
        values = c(1,   1,  -9, 2,  -9, 3,  4,  4,  -9, 5))
    
    Oak
    
       id values
    1   1      1
    2   1      1
    3   1     -9
    4   1      2
    5   1     -9
    6   1      3
    7   1      4
    8   1      4
    9   1     -9
    10  1      5
    
    Oak[ Oak == -9 ] <- NA
    
    Oak
    
       id values
    1   1      1
    2   1      1
    3   1     NA
    4   1      2
    5   1     NA
    6   1      3
    7   1      4
    8   1      4
    9   1     NA
    10  1      5