Creating a Dataframe using a List
R
my_list <- list (names = c ( "Alice" , "Bob" , "Carol" ), ages = c (25, 30, 28)) my_dataframe <- as.data.frame (my_list) print (my_dataframe) |
Output:
names ages
1 Alice 25
2 Bob 30
3 Carol 28
- We start by creating a list named my_list with two named elements: “names” and “ages.” The “names” element contains character values representing names, and the “ages” element contains numeric values representing ages.
- The as.data.frame function takes the list my_list and converts it directly into a dataframe, resulting in the same structured output.
- We then use the as.data.frame() function to convert the my_list into a dataframe. This results in a dataframe with two columns: “names” and “ages.”
- Finally, we print the resulting dataframe. The output displays the data in tabular form, where each row represents a person’s name and age.
How to Convert a List to a Dataframe in R
We have a list of values and if we want to Convert a List to a Dataframe within it, we can use a as.data.frame. it Convert a List to a Dataframe for each value. A DataFrame is a two-dimensional tabular data structure that can store different types of data. Various functions and packages, such as data.frame(), as.data.frame(), and the dplyr package, can be employed to achieve this conversion. In this article, we will discuss how to Convert a List to a Dataframe with its working example in the R Programming Language.