本文介绍了将科学计数法转换为数字,并保留小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,当我用科学计数法转换数字的字符列时,不会保留小数.

For some reason, when I convert a character column of numbers in scientific notation, the decimals aren't preserved.

> str(output)
'data.frame':   213950 obs. of  2 variables:
 $ ColA    : chr  ".3370E+03" ".3375E+03" ".3380E+03" ".3385E+03" ...
 $ ColB    : chr  ".4942E+00" ".5295E+00" ".5682E+00" ".6091E+00" ...

> output$ColA = as.numeric(output$ColA)

> str(output)
'data.frame':   213950 obs. of  2 variables:
 $ ColA    : num  337 338 338 338 339 ...
 $ ColB    : chr  ".4942E+00" ".5295E+00" ".5682E+00" ".6091E+00" ...

我希望它读为:

$ ColA    : num  337 337.5 338 338.5 ...

我尝试了此SO问题的解决方案,但没有运气:

I tried the solution from this SO question, but no luck:

> options(digits=9)
> str(output)
'data.frame':   213950 obs. of  2 variables:
 $ ColA      : num  337 338 338 338 339 ...
 $ ColB      : chr  ".4942E+00" ".5295E+00" ".5682E+00" ".6091E+00" ...

这是怎么回事?

推荐答案

您可以使用以下选项关闭数字的科学计数法;

You can turn off scientific notation for numbers using the option below;

options(scipen = 999)

这将使所有数字都显示为小数.

That would make all the numbers to appear as decimals.

这篇关于将科学计数法转换为数字,并保留小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:58