简体   繁体   English

为什么 as.character 会去掉小数点?

[英]Why does as.character drop decimal point?

I'm interested to know why as.character(5.0) returns 5 but as.character(5.1) returns 5.1 in R.我很想知道为什么as.character(5.0)返回5as.character(5.1)在 R 中返回5.1 I tried to get an answer by reading the documentation but had no luck.我试图通过阅读文档来获得答案,但没有运气。

I'm interested to know why as.character(5.0) returns 5我很想知道为什么 as.character(5.0) 返回 5

The key word here is "returns ."这里的关键词是“回报”。 What do you mean by that?你是什么意思? Note that typing this in the console gives you 5:请注意,在控制台中输入此命令会给您 5:

> 5.0
[1] 5

5 is the same things as 5.0 for the purposes of calculation.为了计算的目的,5 与 5.0 相同。 So what you probably really care about is how 5 is printed .所以你可能真正关心的是如何打印5 。 You thus need to use joran's method or a function like sprintf .因此,您需要使用 joran 的方法或像sprintf这样的 function 。

For more precise formatting of numbers as characters, you might use format :要更精确地将数字格式化为字符,您可以使用format

> format(5,nsmall = 1)
[1] "5.0"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么as.character()在日期列表中返回一个整数? - Why does as.character() return an integer on a list of dates? ffbase :: as.character中的“ by”参数有什么作用? - What does the “by” argument in ffbase::as.character do? 一个不舍入整数的as.character版本 - A version of as.character that does not round integers 神秘:为什么我添加和减去另一个变量时,data.table中的as.character()函数运行得更快? - Mystery: Why does the as.character() function in a data.table run faster if I add and subtract another variable? 为什么在通过 as.character() 将列表强制转换为字符时会出现这种行为? - Why this behavior when coercing a list to character via as.character()? as.character在函数上的用法 - as.character usage on functions as.character的奇怪行为 - Strange behavior with as.character gdata包中的drop.levels(x)和as.factor(as.character(x))有什么区别? - What is the difference between drop.levels(x) in the gdata package and as.factor(as.character(x))? bigmatrix:CreateFileBackedBigMatrix中的错误(as.character(backingfile),as.character(backingpath) - bigmatrix: Error in CreateFileBackedBigMatrix(as.character(backingfile), as.character(backingpath) 重命名创建的对象&lt;-as.character - rename object created <-as.character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM