A list in R is a collection of ordered and changeable data of the same type. A list contains different data types, such as strings or numbers.
The elements contained in a given list are referred to as items.
We use the list()
function to create a list in R. Its parameter values are the items you want in the list.
# creating a string and numerical listsmylist1 <- list("cherry", 'banana', 'orange')mylist2 <- list(1, 2, 3, 4, 5)# to print the listsmylist1mylist2
We create two list
variables, mylist1
and mylist2
. They contain strings and numbers, respectively.
To join or concatenate two different lists in R, we use the c()
function. The c()
function combines the elements of a list by considering the lists as its parameter values.
c(list1, list2)
Note: The number of lists that are concatenated should be two or more.
# creating a string and numerical listsmylist1 <- list("cherry", 'banana', 'orange')mylist2 <- list(1, 2, 3, 4, 5)# joining the listsjointlist <- c(mylist1, mylist2)jointlist
We join the elements of the two lists provided in the code above using the c()
function.