pranav
pranav

Reputation: 460

R : Int vs Num Anomaly in Vector

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

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 ?

Upvotes: 6

Views: 613

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

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.

Upvotes: 5

Related Questions