Home > Software engineering >  Nested ifelse in mutate produce the wrong output
Nested ifelse in mutate produce the wrong output

Time:11-26

I am trying to create a new colum depending on existing column. I want a column infected = 1, when there is at least 1 positive case (meaning nb_positive>0), infected=0 if nb_positive=NA and nb_negative>0 and infected =NA if nb_positive=NA and nb_negative=NA.

The structure of the data is as follow :

structure(list(nb_positif = c(NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 43L, 7L, 2L, NA, NA, NA, 1L, 6L, NA, NA, 2L, NA, 
NA, NA, NA, NA, NA, NA, NA), nb_negatif = c(1L, 2L, 3L, 1L, 1L, 
2L, 6L, 2L, 11L, 1L, 5L, 45L, 35L, 12L, 2L, 3L, 11L, 12L, 9L, 
2L, 2L, 10L, 2L, 14L, 12L, 3L, 2L, 1L, 1L, 15L)), reshapeWide = list(
    v.names = NULL, timevar = "grpName", idvar = "ID", times = c("NEGATIF", 
    "POSITIF", "INDETERMINE"), varying = structure(c("resultatanalyse.NEGATIF", 
    "n.NEGATIF", "resultatanalyse.POSITIF", "n.POSITIF", "resultatanalyse.INDETERMINE", 
    "n.INDETERMINE"), .Dim = 2:3)), row.names = c(1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 14L, 16L, 18L, 19L, 20L, 
21L, 23L, 25L, 26L, 27L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L
), class = "data.frame")

Writing the code below, I see that only the first part is taken into account but not the statement in the second ifelse. Does anyone have an explanation? How can I make my code work?

test_stack %>% mutate(infected=ifelse(nb_positif!="NA",1,
                                            ifelse(nb_negatif!="NA",0,"NA")))
#>    nb_positif nb_negatif infected
#> 1          NA          1       NA
#> 2          NA          2       NA
#> 3          NA          3       NA
#> 4          NA          1       NA
#> 5          NA          1       NA
#> 6          NA          2       NA
#> 7          NA          6       NA
#> 8          NA          2       NA
#> 9          NA         11       NA
#> 10         NA          1       NA
#> 11         NA          5       NA
#> 12         43         45        1
#> 14          7         35        1
#> 16          2         12        1
#> 18         NA          2       NA
#> 19         NA          3       NA
#> 20         NA         11       NA
#> 21          1         12        1
#> 23          6          9        1
#> 25         NA          2       NA
#> 26         NA          2       NA
#> 27          2         10        1
#> 29         NA          2       NA
#> 30         NA         14       NA
#> 31         NA         12       NA
#> 32         NA          3       NA
#> 33         NA          2       NA
#> 34         NA          1       NA
#> 35         NA          1       NA
#> 36         NA         15       NA

Created on 2022-11-25 with reprex v2.0.2

I guess my question is related to this post but I did not manage to make it work. Any help would be very much appreciated.

CodePudding user response:

test_stack %>% mutate(
  infected = case_when((nb_positif > 0) ~ as.character(1),
                       (is.na(nb_positif) & nb_negatif > 0) ~ as.character(0),
                       (is.na(nb_positif) & is.na(nb_negatif)) ~ NA_character_)
)

Note, the outputs for each case must be of the same type. I went with characters because you used that in your other columns, but alternatively you could try this for numeric outputs:

test_stack %>% mutate(
  infected = case_when((nb_positif > 0) ~ 1,
                       (is.na(nb_positif) & nb_negatif > 0) ~ 0,
                       (is.na(nb_positif) & is.na(nb_negatif)) ~ NA_real_)
)
  • Related