Integer class vs Numeric class in R

r

#1

Hello

I am confused between Numeric class and integer class in R. Though, both of them seem to have the same work to do. Yet, we write them separately. I need to know the difference b/w them.

Regards
Nguyin


#2

Hello @copernicus_21,

There are multiple classes that are grouped together as “numeric” classes, the 2 most common of which are float and integer.

int <- c(2,3,4)
class(int)
[1] "numeric"

float <- c(2.5,5.0,7.5)
class(float)
[1] "numeric"

R will automatically convert between the numeric classes during operations,so usually the class is not a matter of concern for the user.
Sometimes you may want to store variables like ID(primary key) specifically as integers if you know no operation will be performed on them or that they will always have integer values.

Also,for the vector float above:

str(as.integer(float))
[1] int [1:3] 2 5 7

So as you can see the numbers have been rounded off to integers with as.integer.
Basically,numeric class can contain both integers and floating numbers but integer class can contain only integers.
Hope this helps!!