R - 将数据框导出到保存数字格式的 txt 文件
R - Export dataframe to txt file keeping number format
我有一个简单的 1 列对象,其中包含许多小数字(数万),这是一个简单的示例
foo <- c(2e-04,0.0099,5e-04,3e-04,0.002,0.0018,6e-04,0.001,3e-04,3e-04,0.005,0.0014,
0.0063,3e-04,0.0054,0.0016,0.0037,0.0071,0.0056,0.0013,0.004,0.0122,0.001,0.0048,
0.0055,0.0039,9e-04,0.004,0.0048,7e-04,0.0013,6e-04,0.0018,0.001,1e-04,6e-04,0.0017,
2e-04,0.0028,0.006,0.0024,0.0026,0.0016,0.0049,0.0043,1e-04,0.0022,4e-04,0.0045,5e-04,
0.0029,3e-04,0.0017,0.0014,0.0078,0.0014,5e-04,0.0081,0.0011,2e-04)
我用formatC
设置格式如下:
foo <- as.numeric(formatC(foo, format = "f", digits = 5, mode = 'real'))
结果为 [1] 0.00020 0.00990 0.00050, 0.00030 ...
。
但是,当我使用 write.table
导出时,结果文件中的数字采用科学记数法。我希望它保持“双”格式。我尝试在 write.table
中使用 formatC
,但(正如预期的那样)它不起作用。
谢谢
简单的代码对我有用
write.table(foo,'file.txt')
生成一个文件,内容为:
"x"
"1" 0.00012
"2" 0.00034
"3" 0.00056
"4" 0.07
好的,我想我明白了,使用格式化功能
write.table(format(foo,digits = 5),"outputfile.txt")
我有一个简单的 1 列对象,其中包含许多小数字(数万),这是一个简单的示例
foo <- c(2e-04,0.0099,5e-04,3e-04,0.002,0.0018,6e-04,0.001,3e-04,3e-04,0.005,0.0014,
0.0063,3e-04,0.0054,0.0016,0.0037,0.0071,0.0056,0.0013,0.004,0.0122,0.001,0.0048,
0.0055,0.0039,9e-04,0.004,0.0048,7e-04,0.0013,6e-04,0.0018,0.001,1e-04,6e-04,0.0017,
2e-04,0.0028,0.006,0.0024,0.0026,0.0016,0.0049,0.0043,1e-04,0.0022,4e-04,0.0045,5e-04,
0.0029,3e-04,0.0017,0.0014,0.0078,0.0014,5e-04,0.0081,0.0011,2e-04)
我用formatC
设置格式如下:
foo <- as.numeric(formatC(foo, format = "f", digits = 5, mode = 'real'))
结果为 [1] 0.00020 0.00990 0.00050, 0.00030 ...
。
但是,当我使用 write.table
导出时,结果文件中的数字采用科学记数法。我希望它保持“双”格式。我尝试在 write.table
中使用 formatC
,但(正如预期的那样)它不起作用。
谢谢
简单的代码对我有用
write.table(foo,'file.txt')
生成一个文件,内容为:
"x"
"1" 0.00012
"2" 0.00034
"3" 0.00056
"4" 0.07
好的,我想我明白了,使用格式化功能
write.table(format(foo,digits = 5),"outputfile.txt")