Scientific notation wrongly converts to numbers

↘锁芯ラ 提交于 2021-01-29 03:53:48

问题


I've got a table of data with scientific notation numbers such as 1.1e-07.
When I try to convert these numbers to normal numbers with as.numeric, R gives me wrong numbers, like:

1.1e-07:   5977  
1.4e-06:   5633  

etc.

How can I fix this?

Update: thanks, it really was about factors.


回答1:


Your numbers are most probably defined as factors. This will result in as.numeric not returning the "value" of the input but the factor level sequence number.

x <- as.factor(c(1.1e-7, 2e-8)) ## convert two numbers to factors

as.numeric(x)
[1] 2 1

To solve this problem make sure you read in the data not as factors from you data source.

Note that a quick and dirty solution would be something like

as.numeric(as.character(x))
[1] 1.1e-07 2.0e-08

format(as.numeric(as.character(x)), scientific = F)
[1] "0.00000011" "0.00000002"

By the way, scientific notation is standard in R. format casts the numbers as character.



来源:https://stackoverflow.com/questions/40655701/scientific-notation-wrongly-converts-to-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!