How to get rid of warnings about NAs?

I'm practicing plot making, and I have some issue with NAs. I get two warning messages:

  1. Expected 2 pieces. Missing pieces filled with NA in 605 rows [189, 268, etc...
  2. In between(hour, 10, 12) : NAs introduced by coercion

This occurs even though I ran the code below before plotting. So how can I solve this NA problem?

df <- df %>% 
  na.omit()

The plot ↓

df %>% 
  separate(Time, c("hour", "minutes"), sep = ":", convert = TRUE) %>% 
  filter(between(hour, 10, 12)) %>% 
  ggplot() +
  aes(hour) +
  stat_count()

Sample data

structure(list(Date = c("9.3.2019", "5.9.2019", "23.12.2019", 
"8.6.2018", "16.1.2019", "19.12.2018", "25.1.2019", "9.10.2014", 
"11.3.2020", "8.5.2017", "9.12.2019", "17.10.2016", "7.12.2018", 
"24.6.2017", "1.10.2018", "24.2.2016", "15.3.2019", "6.3.2020", 
"3.12.2019", "20.1.2020", "25.2.2015", "20.10.2018", "28.3.2019", 
"13.6.2019", "12.5.2016", "8.9.2017", "23.11.2016", "24.7.2015", 
"25.2.2014", "21.7.2019", "22.7.2013", "4.12.2014", "17.3.2016", 
"25.9.2017", "2.12.2016", "23.7.2012", "15.9.2016", "1.1.2018", 
"29.3.2018", "9.11.2017", "7.12.2017", "13.12.2012", "27.12.2016", 
"1.6.2019", "9.12.2012", "14.11.2018", "14.3.2013", "25.8.2016", 
"30.9.2015", "23.6.2015", "3.7.2019", "28.12.2013", "13.3.2014", 
"27.5.2019", "16.5.2016", "10.11.2019", "2.12.2014", "14.12.2015", 
"15.2.2018", "1.2.2015", "16.5.2015", "26.10.2018", "3.1.2016", 
"6.3.2014", "16.11.2015", "9.3.2015", "17.7.2019", "19.5.2017", 
"17.9.2014", "19.2.2017", "26.8.2013", "19.3.2013", "29.3.2019", 
"14.3.2019", "25.1.2015", "11.11.2013", "16.6.2013", "11.2.2018", 
"5.3.2019", "2.7.2017", "10.4.2015", "20.1.2015", "27.7.2017", 
"4.9.2013", "7.9.2016", "16.12.2016", "26.3.2018", "17.12.2014", 
"17.8.2012", "20.4.2013", "4.12.2014", "28.5.2014", "1.6.2012", 
"20.1.2020", "11.9.2015", "16.8.2019", "14.11.2014", "24.1.2015", 
"13.11.2015", "8.2.2020"), Time = c("19:00", "10:00", "17:40", 
"15:00", "16:20", "09:10", "13:00", "-", "19:30", "13:00", "-", 
"09:50", "07:30", "20:00", "10:00", "16:40", "12:40", "12:10", 
"08:30", "14:30", "11:40", "15:00", "09:20", "10:30", "09:30", 
"13:50", "16:40", "18:10", "13:40", "10:00", "-", "13:00", "14:40", 
"08:00", "10:00", "16:20", "09:20", "15:30", "08:20", "12:00", 
"13:30", "16:10", "07:50", "16:20", "19:50", "13:10", "07:10", 
"19:30", "08:30", "22:50", "17:40", "18:00", "08:30", "10:20", 
"13:00", "20:10", "12:10", "10:00", "14:00", "11:20", "08:00", 
"11:40", "11:40", "09:30", "17:30", "00:00", "18:00", "08:10", 
"15:00", "18:30", "13:10", "18:00", "09:00", "08:10", "08:00", 
"16:40", "18:00", "19:10", "13:00", "14:00", "09:10", "10:20", 
"19:40", "12:30", "09:10", "11:30", "13:50", "10:10", "12:10", 
"11:30", "19:30", "12:30", "19:40", "09:20", "13:20", "09:30", 
"11:20", "19:20", "08:40", "07:30")), row.names = c(NA, -100L
), class = c("tbl_df", "tbl", "data.frame"))

This occurs because in some rows you have a "-" instead of a time value, so it can't be separated into hours and minutes, if you want to avoid this simply filter out the rows without time information.

And how to do that? This code didn't do the job

df= df[complete.cases(df),]

Hey @erinho -

A couple of things to try. First, you can use filter to remove those values in Time that are only a hyphen...

df %>% filter(Time != "-")

Alternatively, if you want to drop the rows with NA values after you use separate, you can use drop_na...

df %>% 
  separate(Time, c("hour", "minutes"), sep = ":", convert = TRUE) %>%
  drop_na()

You'll still get a warning from the separate command. If that bugs you, then you should probably use filter first

df %>%
  filter(Time != "-") %>%
  separate(Time, c("hour", "minutes"), sep = ":", convert = TRUE)

Your efforts with na.omit didn't work because to that point, df hadn't been parsed to split up the dates.

Another thought, if you read these values in from a file, you can use read_tsv or read_csv with na = c("", "NA", "-") to automatically turn the - to an NA value from the outset.

1 Like

Hi,

Using suppressWarnings() ?

Lovely, thanks Riffomonas

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.