I want to convert:
library(data.table)
market.cap <- data.table(cap=c("1B", "10M", "2M"))
cap
1 1B
2 10M
3 2M
to:
cap
1 1000000000
2 10000000
3 2000000
Here's my solution. It works, but involves adding a column, which I know isn't necessary. What's a better way?
market.cap[, cap1 := cap]
market.cap$cap = sapply(market.cap$cap, function(x) (as.numeric(temp <- gsub("B", "", x)) * 1000000000))
market.cap$cap1 = sapply(market.cap$cap1, function(x) (as.numeric(temp <- gsub("M", "", x)) * 1000000))
M = data.frame(x = na.omit(market.cap$cap))
B = data.frame(x = na.omit(market.cap$cap1))
rbind(M,B)
We can use gsubfn, match the non-numeric element (\\D), replace that with the corresponding value of the list that matches the key, and use eval(parse to convert that to numeric value.
library(gsubfn)
options(scipen=999)
unname(sapply(gsubfn('\\D', list(B= '*1e9', M= '*1e6'),
market.cap$cap), function(x) eval(parse(text=x))))
#[1] 1000000000 10000000 2000000
We can also use match after extracting the numeric and non-numeric parts, then use match with a vector of letters (c('B', 'M')) to get the numeric index and replace it with new values.
market.cap[, cap1 := as.numeric(sub('\\D', '',
cap))*c(1e9, 1e6)[match( sub('\\d+', '', cap), c('B', 'M'))]]
# cap cap1
#1: 1B 1000000000
#2: 10M 10000000
#3: 2M 2000000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With