List is a type of data objects in R. It can store different types of data objects into one object. You can combine scalars, vectors, matrices, data frames, and even lists into a single list object. A list can be created using list() function in R. The code below shows a simple example to create a list which contains information from scalar, vector and matrix.
#create a scalar a, vector b, numeric matrix c
#and character vector d
a <- "My First List"
b <- c(32, 33, 36)
c <- matrix(1:10, nrow = 2)
d <- c("wang", "zhang", "li")
#create a list mynewlist, with information from
#a,b,c,d. and set name for first two elements in the list
mynewlist <- list(title = a, sum =b, c, d)
#show a list
> mynewlist
$title
[1] "My First List"
$sum
[1] 32 33 36
[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
[[4]]
[1] "wang" "zhang" "li"
It is shown that there are now four elements in the list ‘mynewlist’. When the elements in the list have no name assigned, R will use double bracket [[ ]] to index those elements. Actually double brackets as well as the dollar symbol can be used to index and return the elements of the list, which is shown in the follow code example for family member creation.
#index and return the second element of the list
mynewlist[[2]]
[1] 32 33 36
#index and return the contents of the element assigned with name 'sum'
> mynewlist[["sum"]]
[1] 32 33 36
#index and return the contents of the element assigned with name 'sum'
> mynewlist$sum
[1] 32 33 36
Of course you can assignment each element with corresponding name when creating a list.
# vector with names
name1=c("Wilson","Dudu","Maomao","Miaomiao")
# vector age
age1=c(32,20,22,10)
address vector
address1=c("Aukra","Molde","Molde","Aukra")
# pass these vectors as inputs to the list
familymember1=list(name1= name1,age1= age1,address1= address1)
#show the list contents
print(familymember1)
$name1
[1] "Wilson" "Dudu" "Maomao" "Miaomiao"
$age1
[1] 32 20 22 10
$address1
[1] "Aukra" "Molde" "Molde" "Aukra"
> familymember1
$name1
[1] "Wilson" "Dudu" "Maomao" "Miaomiao"
$age1
[1] 32 20 22 10
$address1
[1] "Aukra" "Molde" "Molde" "Aukra"
You can append a element to an existing list.
#a new vector for name
name2=c("Mico","Mia")
#append name2 to an existing list
familymember1=list(familymember1,name2)
#show list again
familymember1
[[1]]
[[1]]$name1
[1] "Wilson" "Dudu" "Maomao" "Miaomiao"
[[1]]$age1
[1] 32 20 22 10
[[1]]$address1
[1] "Aukra" "Molde" "Molde" "Aukra"
[[2]]
[1] "Mico" "Mia"
You can see, when new element is assigned into an existing list, R will add extra double brackets to the elements in the list.
The contents of a list can be simply modified by using same indexing method as before, with the new value assigned.
#index and return first value of the element 'address1'
familymember1[[1]]$address1[1]
[1] "Aukra"
#assign new value to first value of the element 'address1'
familymember1[[1]]$address1[1]<-'Oslo'
#show list again
familymember1
[[1]]
[[1]]$name1
[1] "Wilson" "Dudu" "Maomao" "Miaomiao"
[[1]]$age1
[1] 32 20 22 10
[[1]]$address1
[1] "Oslo" "Molde" "Molde" "Aukra"
[[2]]
[1] "Mico" "Mia"
For more illustrative knowledge of R, You can watch tutorial videos on our YouTube channel.
Click here to download Python Course Source Files !
For online Python training registration, click here ! Pandas provides flexible ways of generating data…
For online Python training registration, click here ! Data frame is the tabular data object…
Click her for course registration ! When a data frame in Python is created via…
We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…