What's the difference between integer class and numeric class in R

前端 未结 4 1999
深忆病人
深忆病人 2020-12-02 06:25

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 \"

4条回答
  •  醉梦人生
    2020-12-02 07:30

    To quote the help page (try ?integer), bolded portion mine:

    Integer vectors exist so that data can be passed to C or Fortran code which expects them, and so that (small) integer data can be represented exactly and compactly.

    Note that current implementations of R use 32-bit integers for integer vectors, so the range of representable integers is restricted to about +/-2*10^9: doubles can hold much larger integers exactly.

    Like the help page says, R's integers are signed 32-bit numbers so can hold between -2147483648 and +2147483647 and take up 4 bytes.

    R's numeric is identical to an 64-bit double conforming to the IEEE 754 standard. R has no single precision data type. (source: help pages of numeric and double). A double can store all integers between -2^53 and 2^53 exactly without losing precision.

    We can see the data type sizes, including the overhead of a vector (source):

    > object.size(1:1000)
    4040 bytes
    > object.size(as.numeric(1:1000))
    8040 bytes
    

提交回复
热议问题