将带逗号的货币转换为数字 [英] Convert currency with commas into numeric
问题描述
我在数据框中有一个列,如下所示:
I have a column in a dataframe as follows:
COL1
$54,345
$65,231
$76,234
如何将其转换为:
COL1
54345
65231
76234
我最初尝试的方式是:
df$COL1<-as.numeric(as.character(df$COL1))
这没用,因为它说引入了NA 。
That didn't work because it said NA's were introduced.
然后我这样尝试:
df$COL1<-as.numeric(gsub("\\$","",as.character(df$COL1)))
发生了同样的事情。
有什么想法吗?
推荐答案
gsub
不起作用的原因是该列中有,
,仍然是非数字。因此,当使用 as.numeric
转换为数字时,所有非数字元素都将转换为 NA
。因此,我们需要同时删除,
和 $
使其起作用。
The reason why the gsub
didn't work was there was ,
in the column, which is still non-numeric. So when convert to 'numeric' with as.numeric
, all the non-numeric elements are converted to NA
. So, we need to remove both ,
and $
to make it work.
df1$COL1 <- as.numeric(gsub('[$,]', '', df1$COL1))
我们匹配 $
和,
在方括号( [$,]
)内,以便将其视为该字符( $
单独留有特殊含义,即表示字符串的结尾。)并用''
代替。
We match the $
and ,
inside the square brackets ([$,]
) so that it will be considered as that character ($
left alone has special meaning i.e. it signifies the end of the string.) and replace it with ''
.
或者我们可以转义( \\
)字符( $
)以匹配它并替换为''
。
Or we can escape (\\
) the character ($
) to match it and replace by ''
.
df1$COL1 <- as.numeric(gsub('\\$|,', '', df1$COL1))
这篇关于将带逗号的货币转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!