# --6-Lists

\#1-Lists, why would you need them?

```r
#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)

```r
#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
```

&#x20;\#3-Creating a list

```r
#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.

```r
# 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).

```r
# 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.

```r
# 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`.
* Finally, have a look at the structure of `shining_list_full` with the [`str()`](http://www.rdocumentation.org/packages/utils/functions/str) function.

```r
# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://r-pedia.gitbook.io/cevi/r-programming-track/a-introduction-to-r/6-lists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
