How to create R lists
The data structure of a list can be found in many different programming languages. In R, lists are especially useful when you want to summarize data that belongs to different R data types.
What are R lists used for?
Lists are a versatile data structure that is also a separate data type in R. Lists are particularly suitable in dynamic programming environments, when elements of different data types need to be flexibly added and removed. Unlike R vectors, list items don’t care what data type they hold. That’s because the ability to mix data types within a list is one of the main benefits of R lists. Since you can nest lists into each other as you like, it’s possible to display complex issues in the source code using R lists.
If you want to make your R programs available online, try a webspace provider from IONOS. You’ll benefit from at least 50 GB of storage for your projects.
How to create lists in R
list() function
To create a simple R list, you can use the list()
function using the elements to be added to your list.
list <- list(1, TRUE, "Hello world!", c(1,2,3,4))
RIn this code example, a list of four values has been created. As you can see, these values don’t have an identical data type and can still be grouped within a list.
You can also create a list of named items in R. This allows easy access to specific items from your R list.
person <-list(name = "Max White", age = 27, hobbies = c("Programming, movies"))
RYou can also pass other lists as elements to lists, for example, to combine different lists into one record:
people <- list(list(name = "Max White", age = 27, hobbies = c("Programming, movies")), list(name = "Mia Black", age = 32, hobbies = c("Theater, computer games")), list(name = "Mika Brown", age =23, hobbies = c("Sports, music")))
RCreate lists from vectors
You can also create lists from R vectors by converting them to a list. R also provides a function for this.
vector <- c(1,2,3,4)
list <- as.list(vector)
RConvert lists to other data structures
Although R lists are a versatile data structure, you may need other data structures such as vectors or data frames for certain purposes. If this is the case, simply convert your list.
How to turn R lists into vectors
If you want to convert your list to an R vector, use the unlist()
function. This takes an R list as a transfer parameter and converts it into a vector. The list that you pass to the function can also be multidimensional. Often a call to unlist()
is helpful if you want to represent complex hierarchies in a simple vector form.
list <- list(1, 2, 3, 4)
vector <- unlist(list)
RIf you convert a list that contains data from different data types to a vector, you may experience undefined behavior, because vectors in R can only store data of the same data type by default. That’s why a call to unlist()
is only recommended if the elements in your list are of the same data type.
The following code example converts all values from the R vector storage list to the character
data type.
list <- list(1, TRUE, "Hello world!", c(1,2,3,4))
vector <- unlist(list)
RHow to turn R lists into dataframes
R dataframes are used, among other things, for the tabular representation of data sets. That’s why multidimensional lists can be easily converted into R dataframes. But you should note that the lists must have the same number of items. To convert an R list into a dataframe, R provides the function data.frame()
, which expects the list to be converted as a passing parameter.
people <- list(name=c("Max", "Mia", "Mika"), age=c(27, 32, 23))
table <- data.frame(people)
print(table)
RThe dataframe created by the above code looks like this:
name age
1 Max 27
2 Mia 32
3 Mika 23
RAccessing items in R lists
To access items in your list, you could use either the index or, if available, the name of the item you want.
Access via index
To access the list items, you need the R operator [[]]
, into which you insert the index or name.
list <- list(1, TRUE, "Hello world!", c(1,2,3,4))
print(list[[2]])
RThis code returns TRUE
or the second element from the list. Note that indexing in R starts at one. If you already have programming experience and are not just starting and learning to code, errors can quickly occur here, since indexing starts from scratch in most other programming languages.
Access via element name
As mentioned earlier, list items can be named in R. These names can be used for item access if needed. There are two different ways to access elements by name. You can either use double square brackets in combination with the element name as an R string or the dollar sign followed by the element name. Here’s a code sample to help:
person <-list(name = "Max White", age = 27, hobbies = c("Programming, movies"))
print(person[["name"]])
print(person$name)
RIn both cases, the print statement outputs Max White
on the screen.
Key functions of list functions in R
When working with lists, there are a number of useful R functions to help you perform certain operations.
append()
append()
lets you add items to the end of your list. The function assumes the list to which the element is to be appended as the first passing parameter, and the element to be inserted as the second parameter.
list <-list(1,2,3,4)
append(list, 5)
RIf the element to be inserted has a name, you can simply specify it in the function call.
The append()
list function in R isn’t the only way to add items to your list. Often, the index notation with double square brackets, in which you use an index that doesn’t yet exist, works faster:
list <-list(1,2,3,4)
list[[5]] <-5
Rlength()
By calling up the R command length()
you can find out how many items your list contains. The function assumes the list whose length you are interested in as a transfer parameter.
list <- list(1,2,3,4)
print(length(list))
RConcatenation with c()
In R, you can combine two lists into one. For this, you can use the function c()
, which assumes both lists as a parameter and returns a concatenate list.
list1 <- list(1,2,3,4)
list2 <- list(5,6,7,8)
list_concatenate <- c(list1, list2)
Rnames()
If you have created an R list with names, use the R list function names()
to display the names of the objects in your list. The function assumes a list as a transfer parameter.
person <-list(name = "Max White", age = 27, hobbies = c("Programming, movies"))
identifier <- names(person)
R