Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个 csv 文件,其中在 R 中作为列表导出后的价格,措辞如下:115.00 美元,所以我不能用
as.numeric(sub('\\$','',as.character(Data)))
因为由于“美元”部分,它返回 N/As。
删除不是数字或句点的所有内容是最简单的:
> gsub("[^0-9.]", "", "$115.00 USD") [1] "115.00"
如果要确保输入中有连续的字符串,可以使用捕获:
> sub("[^0-9.]*([0-9.]*).*", "\\1", "$115.00 USD") [1] "115.00"