## Converting unit abbreviations to numbers

I have a dataset that abbreviates numerical values in a column. For example, 12M mean 12 million, 1.2k means 1,200. M and k are the only abbreviations. How can I write code that allows R to sort these values from lowest to highest?

I've though about using gsub to convert M to 000,000 etc but that does not take into account the decimals (1.5M would then be 1.5000000).

**The Metric System, Abbreviations, and Unit Conversions,** Percent is not abbreviated, but is represented by the familiar "%" symbol. Always put a space between the number and unit, e.g., 203.65 m, 457 um; pH: The� Quick, free, online unit converter that converts common units of measurement, along with 77 other converters covering an assortment of units. The site also includes a predictive tool that suggests possible conversions based on input, allowing for easier navigation while learning more about various unit systems.

Give this a shot:

Text_Num <- function(x){ if (grepl("M", x, ignore.case = TRUE)) { as.numeric(gsub("M", "", x, ignore.case = TRUE)) * 1e6 } else if (grepl("k", x, ignore.case = TRUE)) { as.numeric(gsub("k", "", x, ignore.case = TRUE)) * 1e3 } else { as.numeric(x) } }

**The Ultimate Guide to SI Units and Unit Conversions,** Quantity, Unit, Abbreviation Converting between the different SI system prefixes is an essential science skill that requires practice. Memorizing the different Larger prefix units always correlate with smaller actual numbers. Note: Converting numbers with decimal places into text values is not consistent across all databases. For more information see Adjustment of Number Format Appears to Round Numbers Incorrectly . Additionally, text (string) manipulation might cause performance issues.

In your case you can using `gsubfn`

a=c('12M','1.2k') dict<-list("k" = "e3", "M" = "e6") as.numeric(gsubfn::gsubfn(paste(names(dict),collapse="|"),dict,a)) [1] 1.2e+07 1.2e+03

**Unit Conversion,** Unit conversion is a multi-step process that involves multiplication or division by a numerical factor, selection of the correct number o. Always put a space between the number and unit, e.g., 203.65 m, 457 um pH: The proper method for reporting pH is to place the unit before the number (pH 7); never insert the word "of" between the unit and number and avoid splitting the unit and number within a sentence - they should always go together.

I am glad to meet you.

I wrote another answer

##### Define function

res = function (x) { result = as.numeric(x) if(is.na(result)){ text = gsub("k", "*1e3", x, ignore.case = T) text = gsub("m", "*1e6", text, ignore.case = T) result = eval(parse(text = text)) } return(result) }

##### Result

> res("5M") [1] 5e+06 > res("4K") [1] 4000 > res("100") [1] 100 > res("4k") [1] 4000 > res("1e3") [1] 1000

**SI Unit rules and style conventions checklist,** Abbreviations, Abbreviations such as sec, cc, or mps are avoided and only are in italic type if they represent variables, quantities, or running numbers. The Excel CONVERT function converts a number from one unit type (e.g. Yards) to another unit type (e.g. Meters). The function is new to Excel 2007, so is not available in earlier versions of Excel. The syntax of the function is: CONVERT(number, from_unit, to_unit)

**Si units conversion worksheet convert between names and ,** si units conversion worksheet convert between names and abbreviations answer key Convert 459 L into milliliters. Metric measurement is based on the number� Choose to have words for the numbers in lowercase, uppercase or title case to easily copy and paste to another application. This converter will convert numbers to words and figures to words. The number to words can be done for real numbers and Scientific E Notation. Limited to use of 90 characters and 1e-90 and 1e+90. References for Number Names

To avoid awkward breaks that maroon the number at the end of a line of text (e.g., 7 mm) and the unit at the beginning of the next line, as in this example, train yourself to type a nonbreaking space between all numbers and units. Do it every time until it’s automatic.

Converting a datetime or timestamp into a timedelta (duration) doesn't make sense. It'd only make sense if you want the duration between the given timestamp, and some other reference date. Then you can get the timedelta just by using - to get the difference between 2 dates.

##### Comments

- You could call numfmt from
`system()`

if it is installed on your system and in the PATH. Something like`system(paste("numfmt --from=auto --to=none", "12M"), intern = TRUE)`

. - Related previous discussion - stackoverflow.com/questions/36806215/… including an answer in the comments which addresses this.
- Also, seems we can safely assume normalized mantissas, in your case (
`12.00k`

or`0.012k`

are not normalized, for example) - Oh and do you care about handling
`NA`

,`NaN`

,`Inf`

without blowing up? - This is exactly the kind of code I was looking for, I didn't think to multiply after changing to numeric values. Only problem is it returns "NA" when it gets to the "k" values. I'm trying to figure out a work around