R:读取 .csv 文件会删除前导零
R: reading in .csv file removes leading zeros
我意识到读取 .csv 文件会删除前导零,但对于我的一些文件,它会保留前导零,而无需我在 read.csv 中显式设置 colClasses。另一方面,让我感到困惑的是在其他情况下,它确实删除了前导零。所以我的问题是:在什么情况下 read.csv 会删除前导零?
read.csv
、read.table
和相关函数将所有内容作为字符串读取,然后根据函数的参数(特别是 colClasses
,还有其他参数)和选项然后函数将尝试 "simplify" 列。如果足够多的列看起来是数字,而你没有告诉函数其他情况,那么它会将它转换为数字列,这将删除任何前导 0(以及小数点后的尾随 0)。如果列中有些东西看起来不像数字,那么它不会转换为数字,而是将其保留为字符或转换为因子,这会保留前导 0。该函数并不总是查看整个列来做出决定,因此对于您来说显然不是数字的内容可能仍会被转换。
最安全(也是最快捷)的方法是指定 colClasses
这样 R 就不需要猜测(并且您不需要猜测 R 将要猜测的内容)。
基本上是对@GregSnow 回答的补充,来自手册。
来自 ?read.csv
的所有引用:
Unless colClasses is specified, all columns are read as character columns and then converted using type.convert to logical, integer, numeric, complex or (depending on as.is) factor as appropriate. Quotes are (by default) interpreted in all fields, so a column of values like "42" will result in an integer column.
另外:
The number of data columns is determined by looking at the first five lines of input...
建议 read.csv
查看前 5 行并从那里猜测该列是否为 numeric
/integer
,否则保持为 character
(因此保持前导 0
).
如果您仍然对更多细节感到好奇,我建议您检查 edit(read.csv)
和 edit(read.table)
中的代码,它们很长但会详细说明函数正在执行的每一步.
最后,顺便说一句,通常最好指定 colClasses
:
Less memory will be used if colClasses is specified as one of the six atomic vector classes. This can be particularly so when reading a column that takes many distinct numeric values, as storing each distinct value as a character string can take up to 14 times as much memory as storing it as an integer.
虽然如果你真的很关心内存 usage/speed,你真的应该使用 fread
from data.table
;即使这样,指定 colClasses
也会产生加速。
我意识到读取 .csv 文件会删除前导零,但对于我的一些文件,它会保留前导零,而无需我在 read.csv 中显式设置 colClasses。另一方面,让我感到困惑的是在其他情况下,它确实删除了前导零。所以我的问题是:在什么情况下 read.csv 会删除前导零?
read.csv
、read.table
和相关函数将所有内容作为字符串读取,然后根据函数的参数(特别是 colClasses
,还有其他参数)和选项然后函数将尝试 "simplify" 列。如果足够多的列看起来是数字,而你没有告诉函数其他情况,那么它会将它转换为数字列,这将删除任何前导 0(以及小数点后的尾随 0)。如果列中有些东西看起来不像数字,那么它不会转换为数字,而是将其保留为字符或转换为因子,这会保留前导 0。该函数并不总是查看整个列来做出决定,因此对于您来说显然不是数字的内容可能仍会被转换。
最安全(也是最快捷)的方法是指定 colClasses
这样 R 就不需要猜测(并且您不需要猜测 R 将要猜测的内容)。
基本上是对@GregSnow 回答的补充,来自手册。
来自 ?read.csv
的所有引用:
Unless colClasses is specified, all columns are read as character columns and then converted using type.convert to logical, integer, numeric, complex or (depending on as.is) factor as appropriate. Quotes are (by default) interpreted in all fields, so a column of values like "42" will result in an integer column.
另外:
The number of data columns is determined by looking at the first five lines of input...
建议 read.csv
查看前 5 行并从那里猜测该列是否为 numeric
/integer
,否则保持为 character
(因此保持前导 0
).
如果您仍然对更多细节感到好奇,我建议您检查 edit(read.csv)
和 edit(read.table)
中的代码,它们很长但会详细说明函数正在执行的每一步.
最后,顺便说一句,通常最好指定 colClasses
:
Less memory will be used if colClasses is specified as one of the six atomic vector classes. This can be particularly so when reading a column that takes many distinct numeric values, as storing each distinct value as a character string can take up to 14 times as much memory as storing it as an integer.
虽然如果你真的很关心内存 usage/speed,你真的应该使用 fread
from data.table
;即使这样,指定 colClasses
也会产生加速。