r - Converting Movie Box Office to Numbers -
i have data frame in r box office number listed $121.5m , $0.014m , i'd convert them straight numbers. i'm thinking of striping $ , m , using basic multiplication. there better way this?
you either matching non-numeric elements ([^0-9.]*) , replace ''
as.numeric(gsub("[^0-9.]*", '', "$121.5m")) #[1] 121.5 or matching $ , m ([$m]) , replace ''
as.numeric(gsub("[$m]", '',"$121.5m")) #[1] 121.5 update
if have vector below
v1 <- c("$1.21m", "$0.5b", "$100k", "$1t", "$0.9p", "$1.5k") create vector numbers , set names corresponding abbrevations
v2 <- setnames(c(1e3, 1e6, 1e9, 1e12, 1e15), c('k', 'm', 'b', 't', 'p')) use index replace abbrevation , multiply numeric part of vector.
as.numeric(gsub("[^0-9.]*", '',v1))* v2[sub('[^a-z]*', '', v1)]
Comments
Post a Comment