从十亿转为百万,反之亦然 [英] Convert from billion to million and vice versa
本文介绍了从十亿转为百万,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下数据框架名为 DF
。我想将收入
列中的所有值转换为同一单位。
Suppose I have the following data frame named DF
. I would like to convert all the values in the Revenue
column to the same unit.
Brands Revenue
A 50.1 bn
B 41.2 bn
C 32.5 Mn
D 15.1 bn
请注意, bn
和 Mn
是向量的一部分。
Please note that bn
and Mn
are part of the vectors.
推荐答案
一个想法, / p>
One idea,
new <- ifelse(gsub('.*\\s+', '', DF$Revenue) == 'bn',
as.numeric(gsub('[A-Za-z]', '', DF$Revenue))*1000, DF$Revenue)
new[!grepl('Mn', new)] <- paste(new[!grepl('Mn', new)], 'Mn', sep = ' ')
DF$Revenue <- new
DF
# Brands Revenue
#1 A 50100 Mn
#2 B 41200 Mn
#3 C 32.5 Mn
#4 D 15100 Mn
相反,
new <- ifelse(gsub('.*\\s+', '', DF$Revenue) == 'Mn',
as.numeric(gsub('[A-Za-z]', '', DF$Revenue))/1000, DF$Revenue)
new[!grepl('bn', new)] <- paste(new[!grepl('bn', new)], 'bn', sep = ' ')
DF$Revenue <- new
DF
# Brands Revenue
#1 A 50.1 bn
#2 B 41.2 bn
#3 C 0.0325 bn
#4 D 15.1 bn
这篇关于从十亿转为百万,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文