0

I need help in converting character values such as "80.5K" or "5.79M" to a numeric data type. I tried using the gsub function, but not sure on how to work with the numbers after the decimal point

gsub(pattern = "K", replacement = "" , "80.53K")
Donald Seinen
  • 4,179
  • 5
  • 15
  • 40
beginner
  • 3
  • 1
  • 1
    Have you tried https://stackoverflow.com/questions/61987917/how-do-i-replace-k-and-m-with-thousands-and-millions or https://stackoverflow.com/questions/56159114/converting-unit-abbreviations-to-numbers ? – Ronak Shah Aug 16 '22 at 03:42
  • Thanks for this one , both the links were helpful – beginner Aug 17 '22 at 09:49

1 Answers1

0

You can use parse_number() from readr library.

library(tidyverse)

value1 <- "80.5K"
parse_number(value1)


value2 <- "5.79M"
parse_number(value2)

Gives:

80.5
5.79

Reference - https://readr.tidyverse.org/reference/parse_number.html

Rahulkg007
  • 67
  • 1
  • 8