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] TRUENotice 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 FALSEThis 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
EasyYou will receive three lines of input:
- A numeric value (e.g.,
99.99) - A text value (e.g.,
Premium Member) - A logical value as text: either
TRUEorFALSE(e.g.,TRUE)
Perform the following operations:
- Read the three inputs and convert them to their appropriate types (numeric, character, and logical)
- Create a list containing these three values in order: the numeric value first, then the text, then the logical value
- 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 - Print the first list
- Print the second list
For example, if the inputs are:
42.5
Active User
FALSEThe 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] TRUELists 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 FALSETry 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 listsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values