0 votes
1 view
in R Programming by (5.3k points)

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

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.

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 returns 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 an integer.

1 Answer

0 votes
by (25.3k points)

The "numeric" class in R has multiple classes grouped under it. The two most common among them are double (for double-precision floating-point numbers) class and integer class.

R automatically converts between the numeric classes when needed. Most math is done using double-precision, so that is often the default storage. And for the most part, it does not matter to a casual user whether the number 3 is currently stored as an integer or as a double. 

Sometimes you may explicitly want to store the elements of 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 easiest to just store them as doubles, to begin with.

Related questions

+3 votes
2 answers
asked Jul 18, 2019 in R Programming by leealex956 (6.5k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...