Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Created February 11, 2016 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aravindhebbali/4a1e731876453e3508c0 to your computer and use it in GitHub Desktop.
Save aravindhebbali/4a1e731876453e3508c0 to your computer and use it in GitHub Desktop.
Working with Lists in R
# example 1: list containing vector and matrix
vect1 <- c('Jack', 'Jill', 'John') # character vector
mat <- matrix(sample(12), nrow = 3) # 3 x 4 matrix
list1 <- list(vect1, mat) # list of 2 objects
list1
# example 2: coerce vector using as.list function
# numeric vector
vect1 <- 1:5
# coerce vect1 into list
list1 <- as.list(vect1)
list1
# lists within list
# character vector
vect1 <- c('Jack', 'Jill', 'John')
# 3 x 4 matrix
mat <- matrix(sample(12), nrow = 3)
# list of 2 objects
list1 <- list(vect1, mat)
# list that contains other lists
list2 <- list(vect1, mat, list1)
list2
# naming list elements
# character vector
vect1 <- c('Jack', 'Jill', 'John')
# 3 x 4 matrix
mat <- matrix(sample(12), nrow = 3)
# named list
list1 <- list(names = vect1, numbers = mat)
list1
# describing lists
# character vector
vect1 <- c('Jack', 'Jill', 'John')
# 3 x 4 matrix
mat <- matrix(sample(12), nrow = 3)
# list of 2 objects
list1 <- list(vect1, mat)
list1
# list containing other list
list2 <- list(names = vect1, numbers = mat, lists = list1)
list2
# class
class(list2)
# length of the list
length(list2)
# names of the elements in the list
names(list2)
# structure of the list
str(list2)
# index/subset list
# character vector
vect1 <- c('Jack', 'Jill', 'John')
# 3 x 4 matrix
mat <- matrix(sample(12), nrow = 3)
# list of 2 objects
list1 <- list(names = vect1, numbers = mat)
list1
# the [] operator
list1[1]
# use the [[]] operator
list1[[1]]
# test the class
class(list1[1])
class(list1[[1]])
# combine [] and [[]] operators
list1
# returns 'Jack'
list1[[1]][1]
# returns 'John'
list1[[1]][3]
# returns element in 2nd row/ 3rd column
list1[[2]][2,3]
# returns all elements of 3rd column of mat
list1[[2]][, 3]
# returns all elements of 3rd row of mat
list1[[2]][3, ]
# the $ operator
list1
# extract the vector
list1$names
# extract the matrix
list1$numbers
# returns 'Jack'
list1$names[1]
# returns 'John'
list1$names[3]
# returns element in 3rd row/ 2nd column
list1$numbers[3, 2]
# returns all elements of 3rd row
list1$numbers[3, ]
# returns all elements of 2nd column
list1$numbers[, 2]
# add/delete list objects
list1
# logical vector
vect2 <- c(TRUE, FALSE, FALSE, TRUE, FALSE)
# add object using $ operator
list1$logic <- vect2
list1
# 3 x3 matrix
mat2 <- matrix(1:9, ncol = 3, byrow = TRUE)
# add object using [[]] operator
list1[[4]] <- mat2
list1
# replace list elements
# replace vect1 with vect2
list1$names <- vect2
list1
# 3 x3 matrix
mat2 <- matrix(1:9, ncol = 3, byrow = TRUE)
# replace mat1 with mat2
list1[[2]] <- mat2
list1
# delete list objects
list1
# delete names vector
list1$names <- NULL
# delete numbers matrix
list1[[2]] <- NULL
list1
# dissolving lists
# character vector
vect1 <- c('Jack', 'Jill', 'John')
# 3 x 4 matrix
mat <- matrix(sample(12), nrow = 3)
# list of 2 objects
list1 <- list(names = vect1, numbers = mat)
list1
# disolve the list using the unlist function
unlist(list1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment