📚 R Programming Tutorial

R Programming

Learn R programming concepts with simple explanations, examples, techniques and practical notes.

← Back to R Programming

Creating Lists in R

A list is a collection of elements of different types. Lists are particularly useful when you need to store heterogeneous data.

In other words, a list is a flexible data structure that can store elements of different types, including numbers, characters, vectors, matrices, other lists, and even functions.

In R, to create a list, we use the list() function and in this, the elements are separated by a comma (,).

Syntax:

list_name <- list(e1, e2, e3, ..)

Examples

Creating a basic List

> std_info <- list(521, "Madhu", c(82, 56, 74), TRUE)
> print(std_info)

Output:

[[1]]
[1] 521

[[2]]
[1] "Madhu"

[[3]]
[1] 82 56 74

[[4]]
[1] TRUE

Creating a Named List

Each element of a list can be given a name. The names are shown with a $ sign in front of them.

> std_info <- list(rno = 521, name = "Madhu", scores = c(82, 56, 74))
> print(std_info)

Output:

$rno
[1] 521

$name
[1] "Madhu"

$scores
[1] 82 56 74