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 lists mylist1 <- list("cherry", 'banana', 'orange') mylist2 <- list(1, 2, 3, 4, 5) # to print the lists mylist1 mylist2
We create two
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.
Note: The number of lists that are concatenated should be two or more.
# creating a string and numerical lists mylist1 <- list("cherry", 'banana', 'orange') mylist2 <- list(1, 2, 3, 4, 5) # joining the lists jointlist <- c(mylist1, mylist2) jointlist
We join the elements of the two lists provided in the code above using the
View all Courses