如何读取以逗号作为小数点分隔符的数字?
我有一系列 CSV 文件,其中数字采用欧洲风格格式,使用逗号而不是小数点,即 0,5
而不是 0.5
。
这些文件太多,无法在导入到 R 之前对其进行编辑。我希望有一个用于 read.csv()
函数的简单参数,或者一种按顺序应用于提取的数据集的方法R 将数据视为数字而不是字符串。
I have a series of CSV files where numbers are formatted in the european style using commas instead of decimal points, i.e. 0,5
instead of 0.5
.
There are too many of these files to edit them before importing to R. I was hoping there is an easy parameter for the read.csv()
function, or a method to apply to the extracted dataset in order for R to treat the data as a number rather than a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当您检查
?read.table
时,您可能会找到您需要的所有答案。(大陆)欧洲 csv 文件存在两个问题:
c
代表什么?对于标准 csv,这是,
,对于欧洲 csv,这是;
sep
是read.table
中对应的参数.
,对于欧洲 csv,这是,
dec
是read.table
中对应的参数要读取标准 csv 使用
read.csv
,要读取欧洲 csv 使用read.csv2
。这两个函数只是设置适当参数的 read.table 的包装器。如果您的文件不遵循这些标准中的任何一个,请手动设置参数。
When you check
?read.table
you will probably find all the answer that you need.There are two issues with (continental) European csv files:
c
in csv stand for? For standard csv this is a,
, for European csv this is a;
sep
is the corresponding argument inread.table
.
, for European csv this is a,
dec
is the corresponding argument inread.table
To read standard csv use
read.csv
, to read European csv useread.csv2
. These two functions are just wrappers toread.table
that set the appropriate arguments.If your file does not follow either of these standards set the arguments manually.
来自
?read.table
:是的,您也可以将其用于
read.csv
。 (对我来说:不傻,你不能!)或者,你也可以使用
它假设一个“,”小数分隔符和一个“;”用于列分隔符。
From
?read.table
:And yes, you can use that for
read.csv
as well. (to me: no stupid, you cannot!)Alternatively, you can also use
which assumes a "," decimal separator and a ";" for column separators.
假设这个导入的字段称为“金额”,如果您的数字被作为字符读取,您可以通过这种方式修复类型:
从 excel 或 excel csv 导入时,我经常遇到这种情况以及其他一些小烦恼。由于似乎没有一致的方法来确保在导入 R 时获得您所期望的结果,因此事后修复似乎是最好的方法。我的意思是,查看您导入的内容 - 确保它是您所期望的,如果不是,请修复它。
Suppose this imported field is called "amount", you can fix the type in this way if your numbers are being read in as character:
I have this happen to me frequently along with a bunch of other little annoyances when importing from excel or excel csv. As it seems that there's no consistent way to ensure getting what you expect when you import into R, post-hoc fixes seem to be the best method. By that I mean, LOOK at what you imported - make sure it's what you expected and fix it if it's not.
可以使用如下:
can be used as follow:
如果您指出缺失值的表示方式(na.strings=...),问题也可能得到解决。例如,这里的 V1 和 V2 具有相同的格式(在 csv 文件中小数点用“,”分隔),但由于 V1 中存在 NA,因此它被解释为因子:
Problems may also be solved if you indicate how your missing values are represented (na.strings=...). For example V1 and V2 here have the same format (decimals separated by "," in csv file), but since NAs are present in V1 it is interpreted as factor:
也许
这也阻止了将字符列转换为因子
maybe
this also prevents to convert the character columns into factors
您可以将十进制字符作为参数传递 (
dec = ","
):有关 https://r-coder.com/read-csv-r/
You can pass the decimal character as a parameter (
dec = ","
):More info on https://r-coder.com/read-csv-r/
只是补充一下布兰登上面的答案,这对我来说效果很好(我没有足够的代表来发表评论):
如果您正在使用,
请不要忘记您可能需要
sub("[.]", " ", d$amount, perl=T)
来绕过.
字符。Just to add to Brandon's answer above, which worked well for me (I don't have enough rep to comment):
If you're using
don't forget that you may need
sub("[.]", "", d$amount, perl=T)
to get around the.
character.