Here, we will discuss lists in R, how to create lists, and check lists.
A list contains heterogeneous data types, hence, its composition is a set of objects that can each be a combination of the numeric (or double), integer, character, logical, complex or raw data types. It has 1 or more levels or hierarchies depending on whether it simply contains a set of objects, or includes another list making it a list of lists.
Create a named list objects including a vector, a matrix, an array, and a dataframe:
[1] "a" "b" "c"
[,1] [,2]
[1,] "a" "x"
[2,] "b" "y"
, , 1
[,1] [,2]
[1,] "a" "c"
[2,] "b" "d"
, , 2
[,1] [,2]
[1,] "e" "g"
[2,] "f" "h"
Team Score Position
1 C 10 3
2 D 7 5
$vector
[1] "a" "b" "c"
$matrix
[,1] [,2]
[1,] "a" "x"
[2,] "b" "y"
$array
, , 1
[,1] [,2]
[1,] "a" "c"
[2,] "b" "d"
, , 2
[,1] [,2]
[1,] "e" "g"
[2,] "f" "h"
$dataframe
Team Score Position
1 C 10 3
2 D 7 5
Create an unnamed list of lists (a list that contains another list):
Color Size
1 Cyan 4
2 Yellow 6
[[1]]
[1] 1 2 3 4
[[2]]
Color Size
1 Cyan 4
2 Yellow 6
[[3]]
[[3]]$vector
[1] "a" "b" "c"
[[3]]$matrix
[,1] [,2]
[1,] "a" "x"
[2,] "b" "y"
[[3]]$array
, , 1
[,1] [,2]
[1,] "a" "c"
[2,] "b" "d"
, , 2
[,1] [,2]
[1,] "e" "g"
[2,] "f" "h"
[[3]]$dataframe
Team Score Position
1 C 10 3
2 D 7 5
Create an empty list:
list()
Add objects to an empty list by assigning objects to some index:
list()
[[1]]
Color Size
1 Cyan 4
2 Yellow 6
[[2]]
NULL
[[3]]
NULL
[[4]]
[1] 1 2 3 4
Add or change names of objects in a list:
$Books
Color Size
1 Cyan 4
2 Yellow 6
$Teacher
NULL
$Class
NULL
$Room
[1] 1 2 3 4
Assign values to an object in a list using index number:
First number (or set of numbers) is for the rows (horizontal), while the second number (or set of numbers) is for the columns (vertical).
[[1]]
Color Size
1 Cyan 4
2 Yellow 6
[[2]]
NULL
[[3]]
NULL
[[4]]
[1] 1 2 3 4
[[1]]
Color Size
1 Blue 4
2 Yellow 5
[[2]]
NULL
[[3]]
NULL
[[4]]
[1] 1 2 3 4
Assign values to an object in a list using index name:
$Books
Color Size
1 Cyan 4
2 Yellow 6
$Teacher
NULL
$Class
NULL
$Room
[1] 1 2 3 4
$Books
Color Size
1 Cyan 4
2 Yellow 6
$Teacher
NULL
$Class
NULL
$Room
[1] 5 6 7 8
Function | Usage |
is.list() |
Check if list |
typeof() |
Check data type |
str() |
Check the structure of a list |
To check if an object is a list:
$vector
[1] "a" "b" "c"
$matrix
[,1] [,2]
[1,] "a" "x"
[2,] "b" "y"
[1] TRUE
To check the data type or mode of a list:
This should return “list” as lists are heterogeneous.
$vector
[1] "a" "b" "c"
$dataframe
Team Score Position
1 C 10 3
2 D 7 5
[1] "list"
Using the str()
function will return the data type or
mode for each list object:
List of 2
$ vector : chr [1:3] "a" "b" "c"
$ dataframe:'data.frame': 2 obs. of 3 variables:
..$ Team : chr [1:2] "C" "D"
..$ Score : num [1:2] 10 7
..$ Position: num [1:2] 3 5
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes