We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !
list is a type of data structure in R programming language. Unlike other data structures, especially matrix and vector, in which each element must has the same type of data, a list provides the flexibility of storing various information in one object. For example, a list may contain vectors, matrices, data frames, and even other lists and that is why many complex R statistical analysis packages often return lists for their function operations.
list() is used to create a list in R. Following example shows creating a list, which stores information of a scalar, a vector, a matrix, and a data frame.
#create a scalar, a vector, a matrix, and a data frame
> a <- "New List"
> b <- c(21, 13, 42)
> c <- matrix(1:100, nrow = 10)
> d <- c("wang", "zhang", "li")
> ID <- c(3, 4, 5, 6)
> age <- c(15, 14, 18, 12)
> blood <- c("Type5", "Type6", "Type5", "Type6")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> pdata <- data.frame(ID, age, blood, status)
#creating a list to contain the information of scalar,
#vector, matrix, data frame created above
#we optinally set names 'title', 'sum', 'e' for
#several objects in this list
> newlist <- list(title = a, sum =b, c, d, e=pdata)
#show this list
> newlist
$title
[1] "New List"
$sum
[1] 21 13 42
[[3]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
[[4]]
[1] "wang" "zhang" "li"
$e
ID age blood status
1 3 15 Type5 Poor
2 4 14 Type6 Improved
3 5 18 Type5 Excellent
4 6 12 Type6 Poor
>
2. Indexing a list
The elements or objects of a list can be indexed by using double brackets with index number, double brackets with object names, or dollar symbol with object names. Following example code shows each of these usage.
#index an object of a list , using double brackets with index
#number
> newlist[[2]]
[1] 21 13 42
> newlist[[3]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
>
#index an object of a list , using double brackets with
#object name
> newlist[["sum"]]
[1] 21 13 42
> newlist[['e']]
ID age blood status
1 3 15 Type5 Poor
2 4 14 Type6 Improved
3 5 18 Type5 Excellent
4 6 12 Type6 Poor
#index an object of a list , using dollar symbol with
#object name
> newlist$e
ID age blood status
1 3 15 Type5 Poor
2 4 14 Type6 Improved
3 5 18 Type5 Excellent
4 6 12 Type6 Poor
> newlist$sum
[1] 21 13 42
>
You can also watch full video on R tutorials from 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…