如何清理包含混合变量的列,例如 char 格式的当前、空白、R 中的字符串

How to clean a column with mixed variables like curreny in char format, blanks, strings in R

我有一个初创公司数据集,其中有一个名为“金额”的列,它只是每个初创公司的估值。当我尝试绘图时,绘图变得丑陋,我发现它们是“char”格式,但是当我尝试使用 table(copy$Amount) 查看列中的值时,它显示所有值相互混合。你可以看到示例图片 here and here

我是 R 的初学者,我尝试了几个小代码但没有任何效果。我想删除“字符串行”、“空白行”和“没有数字的空 $ 符号行”,并将剩余的行转换为数字。

您可以使用 readr 包中的 parse_number,其中:

...drops any non-numeric characters before or after the first number. The grouping mark specified by the locale is ignored inside the number.

例如:

> x <- c("1,000", ",000", "$,000", "1,000$")
> readr::parse_number(x)
[1] 1000 1000 1000 1000