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

查看:335
本文介绍了在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.

推荐答案

有多个类组合在一起为数字类,其中最常见的是double精度浮点数)和整数。 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天全站免登陆