Thursday 12 October 2023

Bigdata-Slip25-Write an R program to create a list of elements using vectors, matrices and a functions. Print the content of the list.

Slip25

Write an R program to create a list of elements using vectors, matrices and a functions. Print the content of the list.

 

# Create a vector

vector_example <- c(1, 2, 3, 4, 5)

 

# Create a matrix

matrix_example <- matrix(1:9, nrow = 3, ncol = 3)

 

# Define a function

create_list <- function() {

  list(

    vector = vector_example,

    matrix = matrix_example

  )

}

 

# Create a list using vectors, matrices, and the function

my_list <- create_list()

 

# Print the content of the list

print(my_list)

 

Out Put

 

> # Create a vector

> vector_example <- c(1, 2, 3, 4, 5)

>

> # Create a matrix

> matrix_example <- matrix(1:9, nrow = 3, ncol = 3)

>

> # Define a function

> create_list <- function() {

+   list(

+     vector = vector_example,

+     matrix = matrix_example

+   )

+ }

>

> # Create a list using vectors, matrices, and the function

> my_list <- create_list()

>

> # Print the content of the list

> print(my_list)

$vector

[1] 1 2 3 4 5

 

$matrix

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

[1,]    1    4    7

[2,]    2    5    8

[3,]    3    6    9