R Lists

A list is a collection that can hold items of different types — numbers, text, logical values, vectors, even other lists. Unlike vectors (which require all elements to be the same type), lists are flexible containers. Think of a list like a filing cabinet where each drawer holds a completely different kind of document.

Vector vs List

Vector (same type only):        List (mixed types allowed):
─────────────────────────────   ──────────────────────────────────
c(10, 20, 30)                  list(10, "hello", TRUE, c(1,2,3))
All numeric                     number + text + logical + vector

Creating a List

person <- list(
  name    = "Kavya",
  age     = 28,
  active  = TRUE,
  scores  = c(85, 92, 78)
)

Structure Diagram

person
├── $name    → "Kavya"         (character)
├── $age     → 28              (numeric)
├── $active  → TRUE            (logical)
└── $scores  → 85, 92, 78     (numeric vector)

Accessing List Elements

# By name (most readable)
person$name          # "Kavya"
person$scores        # 85 92 78

# By name with [[ ]]
person[["age"]]      # 28

# By position with [[ ]]
person[[1]]          # "Kavya"

# Single [ ] returns a sub-list, not the value
person["name"]       # List of 1  ← still a list
person[["name"]]     # "Kavya"    ← the actual value
Indexing difference:
  person["name"]   → returns a LIST containing "Kavya"
  person[["name"]] → returns just "Kavya" (the value itself)

Modifying List Elements

person$age <- 29          # update age
person$city <- "Pune"    # add new element
person$active <- NULL    # remove element

str(person)

Nested Lists

company <- list(
  name = "TechCorp",
  address = list(
    city    = "Bengaluru",
    pincode = "560001"
  ),
  employees = 250
)

company$address$city      # "Bengaluru"
company[["address"]][["pincode"]]  # "560001"

Useful List Functions

Function          Description
─────────────────────────────────────────────────
length(x)         Number of elements in list
names(x)          Names of all elements
str(x)            Compact structure overview
lapply(x, fn)     Apply function to each element, return list
sapply(x, fn)     Apply function, simplify result
unlist(x)         Flatten list into a vector
# Apply mean to each numeric element
data_list <- list(a = c(1,2,3), b = c(4,5,6), c = c(7,8,9))
lapply(data_list, mean)
# $a [1] 2
# $b [1] 5
# $c [1] 8

sapply(data_list, mean)
# a b c
# 2 5 8

Converting a List to a Vector

prices <- list(100, 250, 175, 320)
price_vec <- unlist(prices)
print(price_vec)   # 100 250 175 320
mean(price_vec)    # 211.25

When to Use a List

Use a List When:                        Use a Vector When:
────────────────────────────────────    ──────────────────────────────────
Storing mixed data types                All values are the same type
Grouping related but varied info        Doing math operations
Storing function results (many parts)   Simple ordered collections
Building hierarchical structures        Filtering and comparisons

Lists power many real R workflows — functions return lists when they produce multiple outputs of different types, and data frames are internally stored as lists of equal-length vectors. Understanding lists deeply makes the rest of R much easier to work with.

Leave a Comment

Your email address will not be published. Required fields are marked *