Create a list containing 2 strings, 2 numbers, 2 vectors, 1 list and 2 logical values.
Give names to the elements in the list.
Access the first and second elements of the list.
Add a new item g4 = "Hello" to the list.
Select the second element of the nested list.
Remove the second element of the list.
Create a second list with whatever you want
Merge the two lists into one list.
Print the number of objects in the merged list.
Convert list(1,2,3,4) to a vector
Solution
# Create a list containing 2 strings, 2 numbers, 2 vectors, 1 list and 2 logical values.# Give names to the elements in the list.first_list<-list(string1 ="foo", string2 ="bar", number1 =42, number2 =pi, vec1 =seq(-10,10,1), vec2 =c("Hello", "world"), list1 =list(a =1:10, b =10:1), bool1 =TRUE, bool2 =FALSE)first_list
# Create a second list with whatever you wantsecond_list<-list(a=1:10, b=1:10, c="hello")# Merge the two lists into one list.one_list<-c(first_list, second_list)# Print the number of objects in the merged list.length(one_list)
#> [1] 13
# Convert `list(1,2,3,4)` to a vectoras.numeric(list(1,2,3,4))
# Lists## DefinitionLists allow you to store all types of objects and types of values: booleans, doubles, characters, vectors, other lists, data.frame, etc```{r, warnings=FALSE}# initializationL <- list(name = "John", age = 43, kids = list(name=c("Kevin", "Pamela"), # nested list age =c(4,5) ) )L# names of entries (can be changed)names(L)# statisticssummary(L)str(L)```## Accessing values and other operations```{r, warnings=FALSE}L$name # is a vectorL["age"];typeof(L["age"]) # is a listL[["age"]];typeof(L[["age"]]) # is a vectorL[[3]] # is a list (because 'kids' is a list)L[[3]]['name'] # is a listL[[3]][['name']] # is a vector# empty initializationLL <- list(); LL # no specific sizeLL <- vector("list", length=3); LL # specific size# ConcatenationL1 <- list(wife="Kim", wife.age=38)L2 <- c(L, L1)typeof(L2); L2```## Exercises {#exo-lists}- Create a list containing 2 strings, 2 numbers, 2 vectors, 1 list and 2 logical values.- Give names to the elements in the list.- Access the first and second elements of the list.- Add a new item `g4 = "Hello"` to the list.- Select the second element of the nested list.- Remove the second element of the list.- Create a second list with whatever you want- Merge the two lists into one list.- Print the number of objects in the merged list.- Convert `list(1,2,3,4)` to a vector<details> <summary>Solution</summary>```{r include=TRUE, warning = FALSE, message=FALSE, cache=FALSE}# Create a list containing 2 strings, 2 numbers, 2 vectors, 1 list and 2 logical values.# Give names to the elements in the list.first_list <- list(string1 = "foo", string2 = "bar", number1 = 42, number2 = pi, vec1 = seq(-10,10,1), vec2 = c("Hello", "world"), list1 = list(a = 1:10, b = 10:1), bool1 = TRUE, bool2 = FALSE )first_list# Access the first and second elements of the list.first_list[[1]]first_list[["string2"]]# Add a new item `g4 = "Hello"` to the list.first_list$g4 <- "Hello"first_list# Select the second element of the nested list.first_list[["list1"]][[2]]# Remove the second element of the list.first_list[-2]# Create a second list with whatever you wantsecond_list <- list(a=1:10, b=1:10, c="hello")# Merge the two lists into one list.one_list <- c(first_list, second_list)# Print the number of objects in the merged list.length(one_list)# Convert `list(1,2,3,4)` to a vectoras.numeric(list(1,2,3,4))```</details>