Loading, please wait...

A to Z Full Forms and Acronyms

What is the list and array in the R programming language?

Nov 30, 2021 #RLanguage #Programming, 3868 Views
In this article, we will discuss:What is data structure?How many data structures do we have in the R languageBrief description of two data structures that is list and array.

What is the list and array in the R programming language?

In this article, we will discuss:

  • What is data structure?
  • How many data structures do we have in the R language
  • Brief description of two data structures that is list and array.

Data Structure

It is a technique to store and effectively organize data. It manages the data in a specific format. There are multiple basic and advanced types of data structures available. 

The types of data structures in R:

  • List
  • Array
  • Data Frame
  • Factor
  • R vector

 

List in R

The list is a type of data structure that supports different types of data types. The in-built function list() uses in creating the list and also assigning the values. The data value in the list is called a component. With the help of list indexing, the data elements of the list are accessed. The data types of the component can be of primitive data types such as integer, float, logic, double, character. The component can be other data structures as well. 

The list is one-dimensional in which each data element is the data structure itself. A list can contain data elements of the same and different data types.

Creating a list

#Creating a list with different data types of elements

list1 <- list("Ram", "Green", TRUE, 43.89, 159.1)

print(list1)

When the user executes the above code, it gives the output:

[[1]]

[1] "Ram"

[[2]]

[1] "Green"

[[3]]

[1] TRUE

[[4]]

[1] 43.89

[[5]]

[1] 159.1

Array

An array is a data structure that can be one-dimensional or multidimensional. It stores the data elements of similar data types. The array store the 'n' number of data elements.

One-dimensional arrays are created by using vectors. However, the two-dimensional arrays are created by using matrices. 

The array() function is used for creating the array. The dim keyword is used to define the dimensions of the array. 

Creating the array

array1 <- c(2,5,3)

array2 <- c(10,23,19,29,14,33)

array_result <- array(c(array1,array2),dim = c(3,3,1))

print(array_result)

When the user executes the above code, it gives the output:

, , 1

     [,1] [,2] [,3]

[1,] 2 10 29

[2,] 5 23 14

[3,] 3 19 33

A to Z Full Forms and Acronyms