R: Int vs Num Anomaly in vector

I was working on a R programming tutorial and noticed a slight anomaly:

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

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

+6
source share
1 answer

There are two different problems in the game:

  • In c(2, 1, 1, 5) you explicitly create numeric types. For integer you will need to use c(2L, 1L, 1L, 5L) , since only the suffix L provides the creation of the integer type (or casting via as.integer() , etc.). But read on ...

  • In c(1:5) historical redefinition comes into play for : Since usage almost always includes integer sequences, this is what you get: integers.

Both forms are documented, so this is not an anomaly, as the name of the question implies.

+5
source

Source: https://habr.com/ru/post/985997/


All Articles