rsylatian
rsylatian

Reputation: 429

Convert Mixed Unit Measurements

I have a file with large range of non-standardised mixed imperial and metric measurements, which I want to standardise and republish.

A sample of that range looks like this:

df  <- data.frame(Measurements =c("1.25m", "2 Feet", "3 Inches", "5.5 cm"))

|Measurements|
|1.25m       |
|2 Feet      |
|3 Inches    |
|5.5 cm      |

which I want to look like this:

|Measurements|MM_Conversion|
|1.25m       |1200mm
|2 Feet      |609.6mm
|3 Inches    |76.2mm
|5.5 cm      |55mm

I can't use measurements::conv_unit or units::set_unit because they both seem to require numeric input values. Is there a straightforward way of doing this which can parse both the value and the string, and convert accordingly?

EDIT 1: Having an issue whereby Conv_Unit can't convert NA values. If the initial vector instead was: df <- data.frame(Measurements =c(NA, 1.25m", "2 Feet", "3 Inches", "5.5 cm")), how would you get around it?

Upvotes: 1

Views: 1080

Answers (2)

acylam
acylam

Reputation: 18681

We can use extract from tidyr to separate the value and unit and feed that into conv_unit using map2:

df <- data.frame(Measurements =c(NA, "1.25m", "2 Feet", "3 Inches", "5.5 cm"))

library(tidyverse)
library(stringr)
library(measurements)

df %>%
  extract(Measurements, c("value", "unit"), 
          regex = "^([\\d.]+)\\s*([[:alpha:]]+)$", 
          remove = FALSE, convert = TRUE) %>%
  mutate(unit = str_replace_all(unit, c(Feet="ft", Inches="inch")),
         MM_Conversion = paste0(map2(value, unit, ~if(!is.na(.x)) conv_unit(.x, .y, "mm") else NA), "mm"))

Result:

  Measurements value unit MM_Conversion
1         <NA>    NA <NA>          NAmm
2        1.25m  1.25    m        1250mm
3       2 Feet  2.00   ft       609.6mm
4     3 Inches  3.00 inch        76.2mm
5       5.5 cm  5.50   cm          55mm

or use filter if NAs should not appear in the final output:

df %>%
  extract(Measurements, c("value", "unit"), 
          regex = "^([\\d.]+)\\s*([[:alpha:]]+)$", 
          remove = FALSE, convert = TRUE) %>%
  filter(!is.na(Measurements)) %>%
  mutate(unit = str_replace_all(unit, c(Feet="ft", Inches="inch")),
         MM_Conversion = paste0(map2(value, unit, ~conv_unit(.x, .y, "mm")), "mm"))

Result:

  Measurements value unit MM_Conversion
1        1.25m  1.25    m        1250mm
2       2 Feet  2.00   ft       609.6mm
3     3 Inches  3.00 inch        76.2mm
4       5.5 cm  5.50   cm          55mm

Notice how I manually abbreviated the original units to make conv_unit work. It would be one step less if the original units were already in abbreviated form.

Upvotes: 3

Wimpel
Wimpel

Reputation: 27732

it can be (easily) done, but you have to fix the units in your measurements first, since the accepted length-units from measurements::conv_unit

# accepted units
# $length
# [1] "angstrom" "nm"       "um"       "mm"       "cm"       "dm"       "m"        "km"       "inch"     "ft"       "yd"       "fathom"   "mi"       "naut_mi" 
# [15] "au"       "light_yr" "parsec"   "point" 

so, Inches have to become "inch", and "feet" should become "ft" (perform some regex-magic ;-) ).. but then...

library(tidyverse)
df  <- data.frame( Measurements =c( "1.25m", "2 ft", "3 inch", "5.5 cm" ) )

df %>% 
  #extract the numeric and the unit-parts from the string
  mutate( num_part = as.numeric( stringr::str_extract( Measurements, "\\d+\\.*\\d*" ) ), 
          unit_part = stringr::str_extract( Measurements, "[a-zA-Z]+" ) ) %>%
  #perform a rowwise operation
  rowwise() %>% 
  #convert the units to mm, row-by-row
  mutate( in_mm = conv_unit( num_part, unit_part, "mm" ) )

# Source: local data frame [4 x 4]
# Groups: <by row>
#   # A tibble: 4 x 4
#   Measurements num_part unit_part  in_mm
#   <fct>           <dbl> <chr>      <dbl>
# 1 1.25m            1.25 m         1250  
# 2 2 ft             2    ft         610. 
# 3 3 inch           3    inch        76.2
# 4 5.5 cm           5.5  cm          55  

Upvotes: 4

Related Questions