string R:从字符串转换为双精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26734913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:18:26  来源:igfitidea点击:

R: Converting from string to double

rstringdouble

提问by Mike

I am trying to convert from a string to a double in R. However, every time I convert the number, R creates an integer.

我试图在 R 中从字符串转换为双精度值。但是,每次转换数字时,R 都会创建一个整数。

For example:

例如:

a = "100.11"  
a = as.double(a)  

And the output reads 100. How to I retain the decimals when converting from string to numeric? I've set options(digits=3).

输出为100. 从字符串转换为数字时如何保留小数?我给自己定options(digits=3)

Thanks

谢谢

Mike

麦克风

回答by Roland

The problem is the option you have set:

问题是您设置的选项:

options(digits=3)
as.double("100.11")
#[1] 100
options(digits=5)
as.double("100.11")
#[1] 100.11

digits"controls the number of digits to print when printing numeric values". You set the option to 3 and are shown 3 digits.

digits“控制打印数值时要打印的位数”。您将选项设置为 3 并显示 3 位数字。