How to Declare an Array in R


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,

  1. We declare an array named numbers using the c() function and initialize it with some values.
  2. This creates a numeric vector in R.
  3. 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,

  1. We declare an array named fruits by specifying values directly.
  2. This creates a character vector in R.
  3. 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.




More R Arrays Tutorials

  1. How to Declare an Array in R ?