What is the difference between an entire class and a number class in R

I want a preface to this, saying that I am an absolute beginner in programming, so please excuse how much this is the main question.

I am trying to better understand the "atomic" classes in R, and perhaps this applies to programming classes in general. I understand the difference between character, logical, and complex data classes, but I'm struggling to find a fundamental difference between a number class and an entire class.

Say that I have a simple vector x <- c(4, 5, 6, 6) integers, it would be advisable for him to be an entire 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.

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

+63
object class integer r numeric
May 14 '14 at 16:21
source share
4 answers

There are several classes that are grouped together as β€œnumber” classes, the 2 most common of which are double (for double-precision floating-point numbers) and integers. R will automatically convert between number classes when necessary, so for the most part it doesn't matter to the casual user whether the number 3 is stored as an integer or as a double. Much math is done using double precision, so the default repository is often used.

Sometimes you may need to specifically store the vector as integers if you know that they will never be converted to doubles (used as identifier or index values), since integers require less storage space. But if they are used in any mathematics that converts them into doubles, then it will most likely be easiest to save them as doubles for starters.

+57
May 14 '14 at 17:07
source share

First of all, it is quite possible to successfully use R for years, and there is no need to know the answer to this question. R handles the differences between (regular) numbers and integers for you in the background.

 > is.numeric(1) [1] TRUE > is.integer(1) [1] FALSE > is.numeric(1L) [1] TRUE > is.integer(1L) [1] TRUE 

(Placing the capital letter "L" after an integer forces it to be stored as an integer.)

As you can see, an integer is a subset of a numeric.

 > .Machine$integer.max [1] 2147483647 > .Machine$double.xmax [1] 1.797693e+308 

Integer values ​​are just over 2 billion, while other numbers can be much larger. They may be larger because they are stored as double precision floating point numbers. This means that the number is stored in two parts: the exponent (as 308 is higher, except in base 2, and not in base 10), and β€œsignificant” (as 1.797693 is higher).

Please note that is.integer is not a test of whether you have an integer, but a check of how the data is stored.

It is worth noting that the colon operator : returns integers if the start and end points are integers. For example, 1:5 creates an integer vector of numbers from 1 to 5. You do not need to add the letter L

 > class(1:5) [1] "integer" 

Link: https://www.quora.com/What-is-the-difference-between-numeric-and-integer-in-R

+30
Sep 04 '15 at 15:32
source share

As far as I understand, we do not declare a variable with a data type, so by default R sets any number without L as a number. If you wrote:

 > x <- c(4L, 5L, 6L, 6L) > class(x) >"integer" #it would be correct 

An example of an integer:

 > x<- 2L > print(x) 

Numeric example (sort of like double / float from other programming languages)

 > x<-3.4 > print(x) 
0
09 Oct '18 at 14:24
source share

it's his same

-5
Jan 18 '19 at 8:41
source share



All Articles