Menu
Coddy logo textTech

Creating Lists

Part of the Fundamentals section of Coddy's R journey — lesson 71 of 78.

Vectors are powerful, but they have one limitation: all elements must be of the same type. When you need to store different types of data together, R provides lists.

A list is a flexible data structure that can hold elements of different types, including numbers, characters, logical values, and even other vectors or lists. You create a list using the list() function:

my_list <- list(42, "hello", TRUE)
print(my_list)

Output:

[[1]]
[1] 42

[[2]]
[1] "hello"

[[3]]
[1] TRUE

Notice how each element is displayed with its index in double brackets. This is different from vectors, where elements appear on a single line. Lists can also contain vectors as elements:

my_list <- list(c(1, 2, 3), "text", c(TRUE, FALSE))
print(my_list)

Output:

[[1]]
[1] 1 2 3

[[2]]
[1] "text"

[[3]]
[1]  TRUE FALSE

This ability to mix different data types makes lists ideal for representing complex data structures, like a person's profile containing their age, name, and membership status all in one object.

challenge icon

Challenge

Easy

You will receive three lines of input:

  1. A numeric value (e.g., 99.99)
  2. A text value (e.g., Premium Member)
  3. A logical value as text: either TRUE or FALSE (e.g., TRUE)

Perform the following operations:

  1. Read the three inputs and convert them to their appropriate types (numeric, character, and logical)
  2. Create a list containing these three values in order: the numeric value first, then the text, then the logical value
  3. Create a second list that contains a vector of three numbers c(10, 20, 30) as its first element and the text "data" as its second element
  4. Print the first list
  5. Print the second list

For example, if the inputs are:

42.5
Active User
FALSE

The output should be:

[[1]]
[1] 42.5

[[2]]
[1] "Active User"

[[3]]
[1] FALSE

[[1]]
[1] 10 20 30

[[2]]
[1] "data"

Remember that lists can hold elements of different types, and you can include vectors as list elements.

Cheat sheet

A list is a flexible data structure that can hold elements of different types (numbers, characters, logical values, vectors, or even other lists).

Create a list using the list() function:

my_list <- list(42, "hello", TRUE)
print(my_list)

Output:

[[1]]
[1] 42

[[2]]
[1] "hello"

[[3]]
[1] TRUE

Lists can contain vectors as elements:

my_list <- list(c(1, 2, 3), "text", c(TRUE, FALSE))
print(my_list)

Output:

[[1]]
[1] 1 2 3

[[2]]
[1] "text"

[[3]]
[1]  TRUE FALSE

Try it yourself

# Read the three inputs
con <- file("stdin", "r")
numeric_input <- suppressWarnings(readLines(con, n = 1))
text_input <- suppressWarnings(readLines(con, n = 1))
logical_input <- suppressWarnings(readLines(con, n = 1))

# TODO: Write your code below
# 1. Convert the inputs to their appropriate types (numeric, character, logical)
# 2. Create a list containing these three converted values in order
# 3. Create a second list with c(10, 20, 30) as first element and "data" as second element
# 4. Print both lists
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals