R Programming
  • The wikipedia of R by me
  • Hello R
    • -What is R & RStudio
    • -Learning sources
    • -R online editor
    • -R environment
  • Data types
    • -Dealing with Number
    • -Dealing with String
    • -Dealing with Dates
    • -Dealing with NA's
    • -Dealing with Logicals
    • -Dealing with Factors
  • R data
    • -R object
    • -Data structures
      • --Basics
      • --Managing Vectors
      • --Managing Matrices
      • --Managing Data Frames
    • -Functions
    • -Importing/exporting data
    • -Shape&Transform data
    • -R management
  • Visualizations
  • Intro to R Bootcamp
    • -01-introduction
    • -02-data preparation
    • -03-data transformation
    • -04-visualization
  • R programming track
    • -a-Introduction to R
      • --1-Intro to basics
      • --2-Vectors
      • --3-Matrices
      • --4-Factors
      • --5-Data frames
      • --6-Lists
    • -b-Intermediate R
      • --1-Conditionals and Control Flow
      • --2-Loops
      • --3-Functions
      • --4-The apply family
      • --5-Utilities
    • -d-Writing Functions in R
      • --1-A quick refresher
      • --2-When and how you should write a function
      • --3-Functional programming
      • --4-Advanced inputs and outputs
      • --5-Robust functions
  • Data Wrangling with R
  • R-tutor
    • #R introduction
    • #Elementary Statistics with R
  • Hands-On Programming with R
  • R for Data Science
  • Advanced R
  • ggplot2
  • R packages
  • Statistik-1
  • Statistik-2
  • Statistik-3
  • Zeitreihen & Prognosen
  • Descriptive Analytics
  • Predictive Analytics
  • Prescriptive Analytics
  • R Graphics Cookbook
    • ggplot2 intro
    • ggplot2 custome
    • ggplot top-50
  • #Exploratory Data Analysis
    • -Data Summary
    • -Checklist Solution
  • #Data Mining
    • Untitled
    • Untitled
  • #Machine Learning
    • Intro to ML
    • Intro alghorithms
    • 1. Supervised Learning
  • Master R for Data Science
    • Learning R
    • Untitled
    • Untitled
  • Data Science Projects
    • Simple linear regression:
Powered by GitBook
On this page
  1. R programming track
  2. -a-Introduction to R

--6-Lists

#1-Lists, why would you need them?

#Within a column all elements have the same data type,
#but different columns can be of different data type.

#2-Lists, why would you need them? (2)

#A list in R allows you to gather a variety of 
#objects under one name 
#(that is, the name of the list) in an ordered way. 
#These objects can be matrices, vectors, data frames, 
#even other lists, etc. 
#It is not even required that these objects are related 
#to each other 
#in any way

#3-Creating a list

#Construct a list, named my_list, that contains 
#the variables my_vector, 
#my_matrix and my_df as list components.

# Vector with numerics from 1 up to 10
my_vector <- 1:10 

# Matrix with numerics from 1 up to 9
my_matrix <- matrix(1:9, ncol = 3)

# First 10 elements of the built-in data frame mtcars
my_df <- mtcars[1:10,]

# Construct list with these different elements:
my_list <- list(my_vector,my_matrix,my_df)
my_list

#4-Creating a named list

#Change the code of the previous exercise (see editor) by adding names to the components. Use for #my_vector the name vec, for my_matrix the name mat and for my_df the name df.

#Print out my_list so you can inspect the output.

# Vector with numerics from 1 up to 10
my_vector <- 1:10 

# Matrix with numerics from 1 up to 9
my_matrix <- matrix(1:9, ncol = 3)

# First 10 elements of the built-in data frame mtcars
my_df <- mtcars[1:10,]

# Adapt list() call to give the components names
my_list <- list(my_vector, my_matrix, my_df)
names(my_list)<-c("vec","mat","df")

# Print out my_list
my_list

5-Creating a named list (2)

Complete the code on the right to create shining_list; it contains three elements:

  • moviename: a character string with the movie title (stored in mov)

  • actors: a vector with the main actors' names (stored in act)

  • reviews: a data frame that contains some reviews (stored in rev)

Do not forget to name the list components accordingly (names are moviename, actors and reviews).

# The variables mov, act and rev are available:
mov<-c("The Shining")
act<-c("Jack Nicholson", "Shelley Duvall","Danny Lloyd")
rev<-c("good","bad")

# Finish the code to build shining_list
shining_list <- list(moviename = mov,actors=act,reviews=rev)
shining_list

#6-Selecting elements from a list

  • Select from shining_list the vector representing the actors. Simply print out this vector.

  • Select from shining_list the second element in the vector representing the actors. Do a printout like before.

# shining_list is already pre-loaded in the workspace
# The variables mov, act and rev are available:
mov<-c("The Shining")
act<-c("Jack Nicholson", "Shelley Duvall","Danny Lloyd")
rev<-c("good","bad")

# Finish the code to build shining_list
shining_list <- list(moviename = mov,actors=act,reviews=rev)
shining_list

# Print out the vector representing the actors
shining_list$actors

# Print the second element of the vector representing 
#the actors
shining_list[[2]][2]

shining_list[[2]][3]# in second list element no 3

#7-Adding more movie information to the list

  • Complete the code below such that an item named year is added to the shining_list with the value 1980. Assign the result to shining_list_full.

# shining_list, the list containing movie name, 
#actors and reviews, 
#is pre-loaded in the workspace:
mov<-c("The Shining")
act<-c("Jack Nicholson", "Shelley Duvall","Danny Lloyd")
rev<-c("good","bad")

# Finish the code to build shining_list
shining_list <- list(moviename = mov,actors=act,reviews=rev)
shining_list

# Print out the vector representing the actors
shining_list$actors

# Print the second element of the vector representing 
#the actors
shining_list[[2]][2]

shining_list[[2]][3]# in second list element no 3

# We forgot something; add the year to shining_list
shining_list_full <- c(shining_list,year=1980)

# Have a look at shining_list_full
str(shining_list_full)
shining_list_full
Previous--5-Data framesNext-b-Intermediate R

Last updated 6 years ago

Finally, have a look at the structure of shining_list_full with the function.

str()