将科学记数法转换为数字,保留小数 [英] Convert scientific notation to numeric, preserving decimals

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

问题描述

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

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 ... 

我尝试了 this SO question,但没有运气:

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.

如果要将其恢复为默认值,请使用

If you want to revert it back to the default, use

options(scipen = 0)

查看 getOption("scipen") 了解更多选项.

See getOption("scipen") for more options.

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

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