Create Matrix and Data Frame from Lists in R Programming
Last Updated :
01 Jun, 2020
In
R programming, there 5 basic
objects. Lists are the objects that can contain heterogeneous types of elements, unlike vectors. Matrices can contain the same type of elements or homogeneous elements. On the other hand, data frames are similar to matrices but have an advantage over matrices to keep heterogeneous elements. In this article, we'll learn to create matrix and data frame using lists.
Create Matrix using List
Matrices are created using
matrix()
function in R programming. Another function that will be used is
unlist()
function to convert the lists into a vector. The vector created contains atomic components of the given list.
Syntax:
unlist(x, recursive = TRUE, use.names = TRUE)
Parameters:
x: represents list
recursive: represents logical value. If FALSE, function will not recurse beyond first level of list
use.names: represents logical value to preserve the naming information
Example 1:
Python3
# Defining list
ls1 <- list(
list(1, 2, 3),
list(4, 5, 6))
# Print list
cat("The list is:\n")
print(ls1)
cat("Class:", class(ls1), "\n")
# Convert list to matrix
mt1 <- matrix(unlist(ls1), nrow = 2, byrow = TRUE)
# Print matrix
cat("\nAfter conversion to matrix:\n")
print(mt1)
cat("Class:", class(mt1), "\n")
Output:
The list is:
[[1]]
[[1]][[1]]
[1] 1
[[1]][[2]]
[1] 2
[[1]][[3]]
[1] 3
[[2]]
[[2]][[1]]
[1] 4
[[2]][[2]]
[1] 5
[[2]][[3]]
[1] 6
Class: list
After conversion to matrix:
[, 1] [, 2] [, 3]
[1, ] 1 2 3
[2, ] 4 5 6
Class: matrix
Example 2:
Python3
# Defining list
ls2 <- list("A", 10, TRUE, 2i)
# Print list
cat("\nThe list is:\n")
print(ls2)
cat("Class:", class(ls2), "\n")
# Convert list to matrix
mt2 <- matrix(unlist(ls2), nrow = 2, byrow = TRUE)
# Print matrix
cat("\nAfter conversion to matrix:\n")
print(mt2)
cat("Class:", class(mt2), "\n")
cat("\nType:", typeof(mt2), "\n")
Output:
The list is:
[[1]]
[1] "A"
[[2]]
[1] 10
[[3]]
[1] TRUE
[[4]]
[1] 0+2i
Class: list
After conversion to matrix:
[, 1] [, 2]
[1, ] "A" "10"
[2, ] "TRUE" "0+2i"
Class: matrix
Type: character
Create Dataframe using List
In the same way, dataframe can be created using lists by using
unlist()
function and
data.frame()
function.
Example:
Python3
# Defining lists
n <- list(1:3)
l <- list(letters[1:3])
m <- list(month.name[1:3])
# Convert lists into dataframe columns
df <- data.frame(unlist(n), unlist(l), unlist(m))
# Names of columns of dataframe
names(df) <- c("Number", "Letters", "Month")
# Print dataframe
cat("The dataframe is :\n")
print(df)
Output:
The dataframe is :
Number Letters Month
1 1 a January
2 2 b February
3 3 c March
Similar Reads
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: Python3 1== # R program to convert a data frame # into a nu
2 min read
Convert an Object to Data Frame in R Programming - as.data.frame() Function as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Syntax: as.data.frame(object) Parameters:Â object: Vector, Matrix, factor, or data frameR - as.data.frame() Function ExampleExample 1: Basic exam
2 min read
How to create, index and modify Data Frame in R? In this article, we will discuss how to create a Data frame, index, and modify the data frame in the R programming language. Creating a Data Frame:A Data Frame is a two-dimensional labeled data structure. It may consist of fields/columns of different types. It simply looks like a table in SQL or lik
4 min read
How To Import Data from a File in R Programming The collection of facts is known as data. Data can be in different forms. To analyze data using R programming Language, data should be first imported in R which can be in different formats like txt, CSV, or any other delimiter-separated files. After importing data then manipulate, analyze, and repor
4 min read
Create a matrix from a list of vectors using R In R, vectors can be integer, double, character, logical, complex and raw types. A vector is created using the c() function:a <- c(val1, val2, ...Creating a List of VectorsA list of vectors in R is an array of more than one vector kept in a list structure. Creating a list of vectors is done in th
2 min read
R - Create Dataframe From Existing Dataframe Create Dataframes when dealing with organized data so sometimes we also need to make Dataframes from already existing Dataframes. In this Article, let's explore various ways to create a data frame from an existing data frame in R Programming Language. Ways to Create Dataframe from Existing Dataframe
6 min read