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)
See Question&Answers more detail:os