Convert Data Frame Rows to List in R (Example)
In this post you’ll learn how to store each row of a data frame as a list element in R programming.
Table of contents:
- Creation of Exemplifying Data
- Example: How to Store Each Row of a Data Frame in a List
- Example Continued: Modify Names of List Elements
- Video, Further Resources & Summary
Let’s dive right into the exemplifying R code.
Creation of Exemplifying Data
In the example of this R tutorial, we’ll use the following example data frame:
data <- data.frame(x1 = 1:5, # Create example data x2 = letters[1:5], x3 = "x") rownames(data) <- paste0("row", 1:5) # Specify row names of data data # Print example data # x1 x2 x3 # row1 1 a x # row2 2 b x # row3 3 c x # row4 4 d x # row5 5 e x
As you can see based on the RStudio console output, our data frame contains five rows and three columns. The row names are numerated from row1 to row5.
Example: How to Store Each Row of a Data Frame in a List
If we want to convert the rows of this data frame into list elements, we can use a combination of the split, seq, and nrow functions. Consider the following R code:
data_list <- split(data, seq(nrow(data))) # Convert rows to list data_list # Print list # $`1` # x1 x2 x3 # row1 1 a x # # $`2` # x1 x2 x3 # row2 2 b x # # $`3` # x1 x2 x3 # row3 3 c x # # $`4` # x1 x2 x3 # row4 4 d x # # $`5` # x1 x2 x3 # row5 5 e x
The previous R syntax converted our data frame rows into a list. Please note that the R code of this tutorial is based on a thread on Stack Overflow.
Example Continued: Modify Names of List Elements
You may say that the list we created in Example 1 is difficult to read, since the list elements are just numbered from 1 to 5. With the following R code, we can change the names of our list elements to the row names of our input data:
data_list <- setNames(split(data, # Modify names of list elements seq(nrow(data))), rownames(data)) data_list # $row1 # x1 x2 x3 # row1 1 a x # # $row2 # x1 x2 x3 # row2 2 b x # # $row3 # x1 x2 x3 # row3 3 c x # # $row4 # x1 x2 x3 # row4 4 d x # # $row5 # x1 x2 x3 # row5 5 e x
Video, Further Resources & Summary
I have recently released a video on my YouTube channel, which explains the examples of this article. You can find the video below:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you could have a look at some of the other articles of this website. Some tutorials are listed below.
- Convert List of Vectors to Data Frame in R
- The nrow Command
- The setNames Command
- The R Programming Language
Summary: This article showed how to create a list consisting of the rows of a data frame in the R programming language. Don’t hesitate to let me know in the comments below, if you have additional questions.
2 Comments. Leave new
Leave a Reply Cancel reply
Statistics Globe Newsletter
Very good solution.
Glad to hear that, thanks for the comment Šabo!