Package: readr


Function: parse_number()


1. Extract all numeric values from item1

Review the data (d3)

# A tibble: 4 x 2
     id item1
  <dbl> <chr>
1     1 12cm 
2     2 4cm  
3     3 6cm  
4     4 2.5CM

readr::parse_number() pulls out all numeric values from a string variable and also converts the variable to numeric for us, which is pretty cool.

d3 %>%
  dplyr::mutate(item1 = readr::parse_number(item1))
# A tibble: 4 x 2
     id item1
  <dbl> <dbl>
1     1  12  
2     2   4  
3     3   6  
4     4   2.5

Return to Strings