0

I am new to R.In R how can we distinguish int and float. As in below example we can notice that for data frames cars and iris value of iris$Sepal.Length is double type while value of cars$speed is of int type.

I tried both class and typeof and not able to get required answer here.

> class(iris$Sepal.Length)
 [1] "numeric"
 > class(cars$speed)
[1] "numeric"
> typeof(cars$speed)
[1] "double"
 > typeof(iris$Sepal.Length)
 [1] "double"
> cars$speed
[1]  4  4  7  7  8  9 10 10 10 11 11 12 12 12 12 13 13 13 13 14 14 14 14
[24] 15 15 15 16 16 17 17 17 18 18 18 18 19 19 19 20 20 20 20 20 22 23 24
[47] 24 24 24 25
> iris$Sepal.Length
 [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4
[18] 5.1 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5
HiveRLearner
  • 107
  • 2
  • 10
  • Are u saying that those numeric columns that doesn't have `.` should be `integer` – akrun Jun 07 '17 at 10:35
  • `is.integer(iris$Sepal.Length)` will tell you if this is an integer column. Also `str(cars)` will display the correct classes of all the columns. Also, compare `storage.mode(1) ; storage.mode(1L)`. BTW, `cars$speed` i *not* of integer class- it's just how R prints it. – David Arenburg Jun 07 '17 at 10:43
  • 1
    cars$speed is still stored in a floating point (i.e. numeric or double) vector, even if from a "mathematical point of view" they are integers... you can check if they're "mathematically" integers for instance using `all(abs(as.integer(cars$speed) - cars$speed) <= 1e-12)` – digEmAll Jun 07 '17 at 10:52

0 Answers0