R – Integer Variable

To assign a variable with an integer value in R programming, call as.integer() function, and pass the numeric value to this function. as.integer(x) returns an integer value of x.

We also use the notation L after the numeric value to specify that the number is an integer.

Examples

In the following program, we will create a variable, and assign an integer value to it using as.integer() function.

example.R

x <- as.integer(5)
typeof(x)

Output

[1] "integer"

We have used typeof() function, to print the type of the variable x.

Now, let us use the L notation to define an integer value.

example.R

x <- 5L
typeof(x)

Output

[1] "integer"
ADVERTISEMENT

Conclusion

In this R Tutorial, we learned how to create an integer in R language, using as.integer() function and L notation, with the help of examples.