How to Declare an Array in R - Step by Step Examples
How to Declare an Array in R ?
Answer
In R, you can declare an array using the c() function or by specifying values directly.
✐ Examples
1 Declare an Array using c() Function
In this example,
- We declare an array named
numbers
using thec()
function and initialize it with some values. - This creates a numeric vector in R.
- We print the array elements to the console.
R Program
numbers <- c(1, 2, 3, 4, 5)
print(numbers)
Output
[1] 1 2 3 4 5
2 Declare an Array directly
In this example,
- We declare an array named
fruits
by specifying values directly. - This creates a character vector in R.
- We print the array elements to the console.
R Program
fruits <- c('Apple', 'Banana', 'Orange')
print(fruits)
Output
[1] "Apple" "Banana" "Orange"
Summary
In this tutorial, we learned How to Declare an Array in R language with well detailed examples.