Create a numeric vector in R: using : or c() ?

Did you know in R, : and c() are different when you want to create a numeric vector?

See the example below.

> x <- 1:2
> y <- c(1, 2)
> identical(x, y)
> typeof(x)
[1] "integer"
> typeof(y)
[1] "double"

So, : produces integers while c() produces floating-point number.


Reference: The Art of R Programming by Norman Matloff

Posted in R Programming Tips.

Leave a Reply

Your email address will not be published. Required fields are marked *