first post so please be nice. I am learning my way through R and wanted to know if there's a quick way of converting from k to thousands. For instance, if I have 1.4k it would convert to 1400 or if it is 14k it would be 14000. I have found articles converting the other way but not like this. Would very much appreciate your input. Cheerio, Mari
Asked
Active
Viewed 4,348 times
3
-
More info is needed to provide a quality response, like what else is in the vector besides these two values. Please read about how to ask in the [help]. – Rich Scriven Apr 23 '16 at 03:41
-
HI, they are just a vector of numbers, if they are over 1000 they are represented with k. There are no other conditions, other than the ones I've described. The below answers my question from @kunal pun. Thanks! – maric Apr 23 '16 at 07:16
-
1There is an example of this at the end of section 3 of the gsubfn package vignette which not only handles K (kilo) but also G (giga) and P (peta) and is extendable in an obvious way to as many suffixes as you like: https://cran.r-project.org/web/packages/gsubfn/vignettes/gsubfn.pdf – G. Grothendieck Apr 23 '16 at 11:36
-
`dat <- c("100k", "20K", "15K", "30") ; as.numeric(gsubfn::gsubfn("[kK]$", list(k = "e3", K = "e3"), dat))` to show the utility of Gabor's pkg (for others searching tho find this question). – hrbrmstr Apr 23 '16 at 12:58
-
You could just use sub in that case. Suggest you look at the original example instead which is not so easily handled using a simple sub. – G. Grothendieck Apr 23 '16 at 14:32
1 Answers
14
Another way similar to what @RichardScriven has suggested:
x <- c("1.4k", "14k")
as.numeric(sub("k", "e3", x, fixed = TRUE))
## [1] 1400 14000

Kunal Puri
- 3,419
- 1
- 10
- 22