R中的整数类和数字类有什么区别 [英] What's the difference between integer class and numeric class in R

查看:31
本文介绍了R中的整数类和数字类有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想先说我是一个绝对的编程初学者,所以请原谅这个问题是多么基本.

I want to preface this by saying I'm an absolute programming beginner, so please excuse how basic this question is.

我试图更好地理解 R 中的原子"类,也许这通常适用于编程中的类.我了解字符、逻辑和复杂数据类之间的区别,但我正在努力寻找数字类和整数类之间的根本区别.

I'm trying to get a better understanding of "atomic" classes in R and maybe this goes for classes in programming in general. I understand the difference between a character, logical, and complex data classes, but I'm struggling to find the fundamental difference between a numeric class and an integer class.

假设我有一个简单的整数向量 x <- c(4, 5, 6, 6) ,它是一个整数类是有意义的.但是当我执行 class(x) 时,我得到 [1] "numeric".然后,如果我将此向量转换为整数类 x <- as.integer(x).它返回完全相同的数字列表,但类别不同.

Let's say I have a simple vector x <- c(4, 5, 6, 6) of integers, it would make sense for this to be an integer class. But when I do class(x) I get [1] "numeric". Then if I convert this vector to an integer class x <- as.integer(x). It return the same exact list of numbers except the class is different.

我的问题是为什么会这样,为什么一组整数的默认类是数字类,以及将整数设置为数字而不是整数的优点和或缺点是什么.

My question is why is this the case, and why the default class for a set of integers is a numeric class, and what are the advantages and or disadvantages of having an integer set as numeric instead of integer.

推荐答案

有多个类被组合为数字"类,其中最常见的 2 个是 double(用于双精度浮点数)和 integer.R 会在需要时自动在数字类之间进行转换,因此对于临时用户而言,大多数情况下数字 3 当前存储为整数还是双精度数并不重要.大多数数学是使用双精度完成的,因此这通常是默认存储.

There are multiple classes that are grouped together as "numeric" classes, the 2 most common of which are double (for double precision floating point numbers) and integer. R will automatically convert between the numeric classes when needed, so for the most part it does not matter to the casual user whether the number 3 is currently stored as an integer or as a double. Most math is done using double precision, so that is often the default storage.

有时,如果您知道向量永远不会被转换为双精度值(用作 ID 值或索引),那么您可能希望专门将向量存储为整数,因为整数需要较少的存储空间.但是,如果将它们用于任何将它们转换为双精度的数学中,那么开始时将它们存储为双精度可能是最快的.

Sometimes you may want to specifically store a vector as integers if you know that they will never be converted to doubles (used as ID values or indexing) since integers require less storage space. But if they are going to be used in any math that will convert them to double, then it will probably be quickest to just store them as doubles to begin with.

这篇关于R中的整数类和数字类有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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