R Integer Vectors

R Integer Vector is an atomic vector whose type is “integer”. An integer vector can have integers or NA as items.

In this tutorial, we will learn how to create an integer vector in R, with examples.

Allowed Values in Integer Vector

The following are the list of values allowed in an integer vector.

  • Number followed by literal L.
  • NA

Create an Integer Vector using c()

To create an integer vector, we can use c() function. A number by default is considered double in R. If the number is followed by the character L, then it is an integer.

In the following example, we create an integer vector with length 5. We print the type of vector, and the vector contents.

example.R

</>
Copy
x <- c(2L, 3L, 5L, 7L, 11L)
print(typeof(x))
print(x)

Output

[1] "integer"
[1]  2  3  5  7 11

An integer vector can contain NA (Missing Value).

In the following program, we create an integer vector with some of the values as NA.

example.R

</>
Copy
x <- c(2L, 3L, NA, 7L, NA, 11L)
print(typeof(x))
print(x)

Output

[1] "integer"
[1]  2  3 NA  7 NA 11

Conclusion

In this R Tutorial, we learned what integer vectors are, different values allowed in an integer vector, and how to create an integer vector, with the help of examples.