How do I drop all NA’s in this code?

  • How do I drop all NA’s in this code?

    Posted by Koketso on 23 June 2024 at 15:26

    Good evening

    I’m struggling to remove missing values from my dataset. I want to draw a bar graph without NA.

    Ebola <- rio::import(“linelist_raw.xlsx”)
    glimpse(Ebola)
    Ebola$age <- as.integer(Ebola$age)
    Ebola%>%
    select(age,outcome)%>%
    drop_na(age,outcome)
    Ebola$age_group <- cut(Ebola$age, c(0,9,19,29,39,49,59,69,79,80,Inf),
    c(“0-9″,”10-19″,”20-29″,”30-39″,”40-49″,”50-59″,”60-69”,
    “70-79″,”80-89″,”90+”),include.lowest = TRUE)
    Ebola%>%
    ggplot(aes(age_group,fill = outcome))+
    geom_bar(position = “dodge”)
    Ibrahim replied 1 year, 1 month ago 3 Members · 6 Replies
  • 6 Replies
  • Frank

    Organizer
    23 June 2024 at 15:40

    Hi Koketso,

    You need to drop the NA after grouping. You only removed before, so if any data did not fit in your grouping, may cause further NAs. See the code below.

    After that, in order to do the ggplot, remember to count by the group to determine the frequency of your intended variables so you can further plot and visualise how they look.

    Ebola%>%
    count(age_group,outcome)%>%
    drop_na(age_group, outcome)%>%
    ...
    • Ibrahim

      Moderator
      23 June 2024 at 16:31

      Excellent

  • Ibrahim

    Moderator
    23 June 2024 at 16:35

    @Koketso

    Ebola%>%
    select(age,outcome)%>%
    drop_na(age,outcome)

    There was no assignment on this line so the drop_na never took effect.

    As a bonus, if you want to remove NAs from all columns, you don’t need to specify the columns. In your case, drop_na() will achieve the same effect.

  • Koketso

    Member
    23 June 2024 at 23:20

    Thanks guys, I will try it out.

    • Koketso

      Member
      23 June 2024 at 23:39

      Thanks, it worked

    • Ibrahim

      Moderator
      24 June 2024 at 13:55

      Glad it worked out.
      Try adding these snippets to the code

      .yor plot code + labs(x = “Age Group”, y = “Frequency”, fill = “Outcome”) + theme(legend.position = “bottom”)

      • This reply was modified 1 year, 1 month ago by  Ibrahim.

Log in to reply.