R Program to create a list containing strings, numbers, vectors, logical etc.


February 26, 2023, Learn eTutorial
1770

What is a list in R?

The list is a datatype in R which can able to keep many values of different data type. A list can able to keep the values, strings, matrix, and even a function inside it. 

How to create a list?

This R program helps you to create a list of multiple datatype values. In R, we have a function called list() for doing this functionality. list() function create a list in R that can able to hold values that are of different types.

The syntax of the list() is 

list(1,2,3,...)

  • where the 1, 2, 3,... are list elements

How to create a list of multiple datatype values using the R program?

In this R program, we directly give the values to a built-in function list(). First, we are keeping the values in a list called datalist. Then call the function list() for making the values list with different types. Finally, print the variable datalist that contains the lists.

ALGORITHM

STEP 1: Assign variables datalist with a list of values

STEP 2: Call the list() function

STEP 3: print the variable datalist variable


For getting a better understanding of this R program, please refer to our page that explains the Lists

R Source Code

                                          datalist = list("Java", "PHP", c(2, 4, 6, 8), TRUE, 20.19, 62.54)
print("Data of the list:")
print(datalist)
                                      

OUTPUT

[1] "Data of the list:"
[[1]]
[1] "Java"

[[2]]
[1] "PHP"

[[3]]
[1]  2  4  6 8

[[4]]
[1] TRUE

[[5]]
[1] 20.19

[[6]]
[1]62.54