6

I was working my way through a primer on R programming and noticed a slight anomaly :

  • x <- c(2,1,1,5) produces a vector of type num
  • y <- c(1:5) produces a vector of type int
  • z <- c(1.5,2.3) produces a vector of type num

Why does this happen ? What is the fundamental data type in R : is it int or is it num ? What happens if one of the elements in the vector is a float , does the type of the vector become float or is it something else ? What happens when all the elements in the vector are float - why is it still num in that case ?

pranav
  • 450
  • 7
  • 15
  • 3
    All numbers are "double"s (`typeof(2)`) unless specified otherwise (`as.integer` or `L`). `c` is a function that concatenates and coerces to highest `typeof` where in your "x" is "double". `:` is a function that returns an "integer" vector by design. Also, "numeric" is, also, a class and returns `TRUE` for "integer"s and "double"s (see `?numeric`). – alexis_laz Apr 26 '15 at 15:06
  • 3
    From the **Value** section of the help file (`?":"`), `For numeric arguments, a numeric vector. This will be of type integer if from is integer-valued and the result is representable in the R integer type, otherwise of type "double" (aka mode "numeric").` – nrussell Apr 26 '15 at 15:10

1 Answers1

5

There are two distinct issue at play:

  1. In c(2, 1, 1, 5) you are explicitly creating numeric types. For integer, you would have to use c(2L, 1L, 1L, 5L) as only the suffix L ensures creation of an integer type (or casting via as.integer() etc). But read on ...

  2. In c(1:5) a historical override for the : comes into play. Because the usage almost always involves integer sequences, this is what you get: integers.

Both forms are documented, so it is not an anomaly as your question title implies.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Hi , @DirkEddelbuettel , usually in languges like `Python` etc. `L` is reserved for `int : type long` , why is it that `R` uses `L` for type-casting to `int` . Also , which method should be preferred in practice according to you - `c()` or `c(:)` ? Thanks a ton :) – pranav Apr 26 '15 at 15:14
  • 4
    Python practice is irrelevant here. Have a look at eg the [R Language Definition manual](http://cran.r-project.org/doc/manuals/r-release/R-lang.html). Moreover, R only has `integer` and no distinction between different sizes of integers (ie no `long int` or `long long int`). – Dirk Eddelbuettel Apr 26 '15 at 15:16
  • Thanks a ton @DirkEddelbuettel , I got it , was just wondering as to why does `R` follow a different practice in using `L` compared to other languages like Python , C/C++ , Java etc. Thanks again :) – pranav Apr 26 '15 at 15:40
  • 2
    R follows S conventions. S is is older than Python so the question might be why did Python deviate from existing practice. – IRTFM Apr 26 '15 at 16:10
  • @BondedDust , but don't languages like C/C++ , Java also follow the convention of using `l or L` for long ? – pranav Apr 27 '15 at 05:27
  • 2
    I'm having a hard time thinking there much more to say on this topic. R is not going to change. – IRTFM Apr 27 '15 at 07:46