w3resource

R Programming - Convert DataFrame to list by rows

R Programming: List Exercise-12 with Solution

Write a R program to convert a given dataframe to a list by rows.

Sample Solution :

R Programming Code :

# Create a data frame with student exam data
exam_data = data.frame(
  name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'),
  score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
  attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
  qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)

# Print the original data frame
print("Original dataframe:")
print(exam_data)

# Convert each row of the data frame to a separate list element
new_list = split(exam_data, seq(nrow(exam_data)))

# Print the list created from the data frame rows
print("dataframe rows to a list:")
print(new_list)

Output:

[1] "Original dataframe:"
        name score attempts qualify
1  Anastasia  12.5        1     yes
2       Dima   9.0        3      no
3  Katherine  16.5        2     yes
4      James  12.0        3      no
5      Emily   9.0        2      no
6    Michael  20.0        3     yes
7    Matthew  14.5        1     yes
8      Laura  13.5        1      no
9      Kevin   8.0        2      no
10     Jonas  19.0        1     yes
[1] "dataframe rows to a list:"
$`1`
       name score attempts qualify
1 Anastasia  12.5        1     yes

$`2`
  name score attempts qualify
2 Dima     9        3      no

$`3`
       name score attempts qualify
3 Katherine  16.5        2     yes

$`4`
   name score attempts qualify
4 James    12        3      no

$`5`
   name score attempts qualify
5 Emily     9        2      no

$`6`
     name score attempts qualify
6 Michael    20        3     yes

$`7`
     name score attempts qualify
7 Matthew  14.5        1     yes

$`8`
   name score attempts qualify
8 Laura  13.5        1      no

$`9`
   name score attempts qualify
9 Kevin     8        2      no

$`10`
    name score attempts qualify
10 Jonas    19        1     yes                         

Explanation:

  • Create a Data Frame:
    • exam_data = data.frame(...)
      Creates a data frame exam_data with columns for student names, scores, attempts, and qualification status.
  • Print Original Data Frame:
    • print("Original dataframe:")
      Prints a message indicating that the original data frame will be displayed.
    • print(exam_data)
      Displays the contents of the exam_data data frame.
  • Convert Data Frame to List:
    • new_list = split(exam_data, seq(nrow(exam_data)))
      Converts the data frame into a list where each element of the list is a single row of the data frame. The split function splits the data frame into list elements based on row numbers.
  • Print New List:
    • print("dataframe rows to a list:")
      Prints a message indicating that the converted list will be displayed.
    • print(new_list)
      Displays the list where each element represents a row from the original data frame.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to count number of objects in a given list.
Next: Write a R program to convert a given matrix to a list.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/list/r-programming-list-exercise-12.php