Show which elements of a column cannot be converted to numeric in R

Viewed 57

The problem:

I have a tibble column that I wish to convert to a numeric column. Most of the cells can be converted easily (i.e. "1" becomes 1, "NA" becomes NA). Some however cannot be converted easily (e.g. "1-4"), in these cases I am understandably given a "NAs introduced by coercion" error.

Is there a simple way or function (preferably in tidyverse style/syntax) that can show the rows that which were not easily converted to numeric.

Reproducible example:

tibble::tibble(x = 1:10, 
               y = c("11", "12", "13", "14", "15", "16", "17", "18-21", "22", "23")) %>% 
  dplyr::mutate(y = as.numeric(y))

The output I looking for in this case is:

x y
8 8 "18-21"
1 Answers

We need a filter on the NA elements after converting to numeric as non-numeric elements are converted to NA which is picked up by is.na as a logical vector and this can be used in filter to get those rows

library(dplyr)
tibble::tibble(x = 1:10, 
                y = c("11", "12", "13", "14", "15", "16", "17", "18-21", "22", "23")) %>% 
     mutate(rn = row_number()) %>%
     filter(is.na(as.numeric(y)))

-output

# A tibble: 1 x 3
      x y        rn
  <int> <chr> <int>
1     8 18-21     8

With as.numeric, there is a friendly warning that says the function returns NA for some elements. If we want to avoid that, use a regex pattern to filter those rows i.e. \\D - will look for any non-digits in the dataset

library(stringr)
tibble::tibble(x = 1:10, 
                y = c("11", "12", "13", "14", "15", "16", "17", "18-21", "22", "23")) %>% 
     mutate(rn = row_number()) %>%
     filter(str_detect(y, '\\D'))

-output

# A tibble: 1 x 3
      x y        rn
  <int> <chr> <int>
1     8 18-21     8
Related