在.csv中读取的正确小数位数 [英] Correct number of decimal places reading in a .csv

查看:2084
本文介绍了在.csv中读取的正确小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv,其中一列包含具有7位小数的​​数字,例如: -117.2403266



当我将 .csv 读入R时,它只显示该列的4个小数位,例如: -117.2403 。或者也许他们都在那里,但当我打印它只显示四个小数位?



我认为这可能是在 read.csv()函数,但它没有说明小数位。

解决方案

> read.csv 不是截断或舍入,但您的 print.data.frame 函数只显示值在 options()中指定的精度。尝试:

  print(dfrm,digits = 10)

> dfrm <-data.frame(test = -117.2403266)
> print(dfrm)
test
1 -117.2403
> print(dfrm,digits = 10)
test
1 -117.2403266

格式将显示精度没有丢失,但它会返回一个字符向量,因此它可能不适合分配当一个数字值期望。 / p>

编辑一个2岁的帖子:本主题可能会提出有关如何导入整数的问题,当它们大于 .Machine $因为它们现在可以在内部完全存储为'数字'横坐标值,所以最大值将是2 ^ 52(或2 ^ 53-1,I忘记它是)。当从基于 scan 的函数(如 read。* -family)中读取这些函数时,您需要声明为numeric而不是integer:

 > str(scan(text =21474836470,what = integer()))
scan中的错误(file,what,nmax,sep,dec,quote,skip,nlines,na.strings,:
scan()期望的'整数',得到'21474836470'
> str(scan(text =21474836470,what = numeric()))
阅读1项
num 2.15e +10
> str(read.table(text =21474836470,colClasses =integer))
扫描时出错(file,what,nmax,sep,dec,quote,skip,nlines ,na.strings,:
scan()expected'an integer',got'21474836470'
> str(read.table(text =21474836470,colClasses =numeric))
'data.frame':1 obs。of 1 variable:
$ V1:num 2.15e + 10


b $ b

如果不为what指定类型或模式, scan 将假设 numeric(),它会成功。


I have a .csv where one of the columns contains numbers that have 7 decimal places, e.g.: -117.2403266.

When I'm reading the .csv into R it only ever shows 4 decimal places for that column, e.g.: -117.2403. Or maybe they are all there but when I print it only shows the four decimal places?

I thought that this might be solved within the arguments of the read.csv() function, but it doesn't say anything about decimal places.

解决方案

read.csv is not truncating or rounding, but your print.data.frame function is only displaying the values to the precision specified in options(). Try:

 print(dfrm, digits=10)

> dfrm<- data.frame(test=-117.2403266)
> print(dfrm)
       test
1 -117.2403
> print(dfrm, digits=10)
          test
1 -117.2403266

Using format as suggested would show that the precision has not been lost, but it would return a character vector, so it might not be suitable for assignment when a numeric value was expected.

Edit of a 2 yr-old post: This topic might bring up the question regarding how integers can be imported when they are larger than .Machine$integer.max #[1] 2147483647, since such they can now be internally stored exactly as 'numeric'-abscissa values, so that maximum would be 2^52 (or 2^53-1, I forget which it is). When these are read in from a scan-based function (as are all 0f the read.*-family), you would need to declare as 'numeric' rather than 'integer':

> str( scan(text="21474836470", what=integer()))
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  scan() expected 'an integer', got '21474836470'
> str( scan(text="21474836470", what=numeric()))
Read 1 item
 num 2.15e+10
> str( read.table(text="21474836470", colClasses="integer"))
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  scan() expected 'an integer', got '21474836470'
> str( read.table(text="21474836470", colClasses="numeric"))
'data.frame':   1 obs. of  1 variable:
 $ V1: num 2.15e+10

If you don't specify a type or mode for "what", scan would assume numeric() and it would succeed.

这篇关于在.csv中读取的正确小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆