2  Data Structures

There are five main data structures in R. They are:

  1. vectors

  2. matrix

  3. array

  4. data frame

  5. list

2.1 Vectors

  1. One dimensional data object.

  2. Homogeneous data structure. That means data in a vector must only be one type or mode (numeric, character, or logical). You cannot mix different types of data. If you try to mix different types of data, R will automatically convert them into one type.

2.1.1 Creating Vectors

Vectors can be made in four primary ways. They are

  1. using c() function

  2. using : function

  3. using seq function

  4. using rep function

Methods ii–iv simplify vector creation. They are useful when there is a pattern in data.

2.1.2 Concatenate: c()

syntax:

Example:

The following will create the vector but not assigned a name.

c(1996, 1998, 2000, 2005)
[1] 1996 1998 2000 2005

Assigning a name to vector:

The advantage of assigning a name is that we can reuse the same set of values by calling the vector name.

a <- c(1996, 1998, 2000, 2005)
a
[1] 1996 1998 2000 2005

2.1.3 Colon: :

The : function can be used to create a regular decreasing or increasing sequence.

Examples:

1:10
 [1]  1  2  3  4  5  6  7  8  9 10
10:1
 [1] 10  9  8  7  6  5  4  3  2  1
-0.5:10
 [1] -0.5  0.5  1.5  2.5  3.5  4.5  5.5  6.5  7.5  8.5  9.5
-0.3:10
 [1] -0.3  0.7  1.7  2.7  3.7  4.7  5.7  6.7  7.7  8.7  9.7

In all of the above sequences the increment is one. The output will display the numbers only within the range.

2.1.4 Sequence: seq

seq function cal also be used for creating regular sequence. With seq you can control the increment and length of the output.

Example 1

seq(1, 19)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19

Example 2

seq(1, 19, length.out=8)
[1]  1.000000  3.571429  6.142857  8.714286 11.285714 13.857143 16.428571
[8] 19.000000

Example 3

seq(1, 19, by = 3)
[1]  1  4  7 10 13 16 19

2.1.5 Repeat: rep

The rep function can be used if there is a pattern of repetition in the data.

Example 1

The number 8 is repeated three times.

rep(8, 5)
[1] 8 8 8 8 8

Example 2

The sequence 1, 2, 3 is repeated five times.

rep(1:3, times=5)
 [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3

Example 3

Same as in Example 2 above.

rep(1:3, 5)
 [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3

Example 4

Each element in the sequence is repeated five times.

rep(1:3, each=5)
 [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3

Example 5

First, each element is repeated five times. After that, the whole sequence is repeated three times.

rep(1:3, each=5, times=3)
 [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2
[39] 2 2 3 3 3 3 3

Example 6

Same as before. Changing the ordering of each and time does not change the output.

rep(1:3, times=3, each=5)
 [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2
[39] 2 2 3 3 3 3 3

2.1.6 Coercion

When you try to include different types they will be coerced to the most flexible type.

a <- c(1, 3, "GPA", TRUE, 1L)
typeof(a)
[1] "character"

Explicit coercion means that if we try to convert a data type to another data type intentionally using a specific function. For example,

b <- c(3.1, 3.2, 3.7, 5.9)
b
[1] 3.1 3.2 3.7 5.9
as.integer(b)
[1] 3 3 3 5

2.1.7 Functions that can be used to inspect vectors

Consider the vector below

example.vec <- c(1,  2,  3, 4, 5, 6, 7, 8)
  1. To check the storage mode
typeof(example.vec)
[1] "double"
  1. To check the class type
class(example.vec)
[1] "numeric"
  1. Testing functions
is.character(example.vec)
[1] FALSE
is.integer(example.vec)
[1] FALSE
is.logical(example.vec)
[1] FALSE
is.double(example.vec)
[1] TRUE
  1. Mathematical and statistical functions
sum(example.vec)
[1] 36
mean(example.vec)
[1] 4.5
summary(example.vec)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    2.75    4.50    4.50    6.25    8.00 
  1. To check if there are any missing values
is.na(example.vec)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

There are many more functions that you can use with vectors. We will learn about them in the upcoming chapters.

2.1.8 Exercise

  1. Write R codes to create the following vectors: If you see patterns in the data, use vector simplification methods.
[1] 1990 1992 1934 1957 1970 2000 2005
 [1] 3 6 9 3 6 9 3 6 9 3 6 9 3 6 9
 [1] 3 3 3 3 3 6 6 6 6 6 9 9 9 9 9
 [1] 3 3 3 3 3 6 6 6 6 6 9 9 9 9 9 3 3 3 3 3 6 6 6 6 6 9 9 9 9 9
 [1]  1  4  7 10 13 16 19 22 25 28 31 34
  [1] 0.1000000 0.1020202 0.1040404 0.1060606 0.1080808 0.1101010 0.1121212
  [8] 0.1141414 0.1161616 0.1181818 0.1202020 0.1222222 0.1242424 0.1262626
 [15] 0.1282828 0.1303030 0.1323232 0.1343434 0.1363636 0.1383838 0.1404040
 [22] 0.1424242 0.1444444 0.1464646 0.1484848 0.1505051 0.1525253 0.1545455
 [29] 0.1565657 0.1585859 0.1606061 0.1626263 0.1646465 0.1666667 0.1686869
 [36] 0.1707071 0.1727273 0.1747475 0.1767677 0.1787879 0.1808081 0.1828283
 [43] 0.1848485 0.1868687 0.1888889 0.1909091 0.1929293 0.1949495 0.1969697
 [50] 0.1989899 0.2010101 0.2030303 0.2050505 0.2070707 0.2090909 0.2111111
 [57] 0.2131313 0.2151515 0.2171717 0.2191919 0.2212121 0.2232323 0.2252525
 [64] 0.2272727 0.2292929 0.2313131 0.2333333 0.2353535 0.2373737 0.2393939
 [71] 0.2414141 0.2434343 0.2454545 0.2474747 0.2494949 0.2515152 0.2535354
 [78] 0.2555556 0.2575758 0.2595960 0.2616162 0.2636364 0.2656566 0.2676768
 [85] 0.2696970 0.2717172 0.2737374 0.2757576 0.2777778 0.2797980 0.2818182
 [92] 0.2838384 0.2858586 0.2878788 0.2898990 0.2919192 0.2939394 0.2959596
 [99] 0.2979798 0.3000000
 [1] -0.5  0.5  1.5  2.5  3.5  4.5  5.5  6.5  7.5  8.5  9.5 10.5
 [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
[26] 52 54 56 58 60 62 64 66 68 70 72
  1. Use the typeof() function to check the R storage mode of the following vectors and class() to check the class type of the vector.
logical_vector <- c(TRUE, FALSE, TRUE, FALSE)
integer_vector <- c(1L, 2L, 3L, 4L)
double_vector <- c(1.1, 2.2, 3.3, 4.4)
complex_vector <- c(1+1i, 2+2i, 3+3i, 4+4i)
character_vector <- c("a", "b", "c", "d")
null_vector <- NULL
time_data <- 1996:2006
time_series_data <- ts(1996:2006)
  1. Create the vector (3, 3, 3, . . . 3, 6, 6, . . . 6, 9, 9, 9, . . . 9), where there are 10 occurrences of 3, 20 occurrences of 6 and 30 occurrences of 9.

  2. Find the value of the following expression.

  1. \(\sum_{i=1}^{100}i\)

  2. \(\sum_{i=1}^{100}i^2\)

  1. Generate a sequence using the code seq(from=1, to=10, by=1). What other ways can you generate the same sequence?

  2. Create a vector to hold population values, and label each element with the corresponding province name. The plot will display population values when hovered over.

2.1.9 Vector Operations

Vector operations are performed element by element.

a <- 1:10
b <- rep(10:100, by=10)
a + b
Warning in a + b: longer object length is not a multiple of shorter object
length
 [1]  11  13  15  17  19  21  23  25  27  29  21  23  25  27  29  31  33  35  37
[20]  39  31  33  35  37  39  41  43  45  47  49  41  43  45  47  49  51  53  55
[39]  57  59  51  53  55  57  59  61  63  65  67  69  61  63  65  67  69  71  73
[58]  75  77  79  71  73  75  77  79  81  83  85  87  89  81  83  85  87  89  91
[77]  93  95  97  99  91  93  95  97  99 101 103 105 107 109 101
a/b
Warning in a/b: longer object length is not a multiple of shorter object length
 [1] 0.10000000 0.18181818 0.25000000 0.30769231 0.35714286 0.40000000
 [7] 0.43750000 0.47058824 0.50000000 0.52631579 0.05000000 0.09523810
[13] 0.13636364 0.17391304 0.20833333 0.24000000 0.26923077 0.29629630
[19] 0.32142857 0.34482759 0.03333333 0.06451613 0.09375000 0.12121212
[25] 0.14705882 0.17142857 0.19444444 0.21621622 0.23684211 0.25641026
[31] 0.02500000 0.04878049 0.07142857 0.09302326 0.11363636 0.13333333
[37] 0.15217391 0.17021277 0.18750000 0.20408163 0.02000000 0.03921569
[43] 0.05769231 0.07547170 0.09259259 0.10909091 0.12500000 0.14035088
[49] 0.15517241 0.16949153 0.01666667 0.03278689 0.04838710 0.06349206
[55] 0.07812500 0.09230769 0.10606061 0.11940299 0.13235294 0.14492754
[61] 0.01428571 0.02816901 0.04166667 0.05479452 0.06756757 0.08000000
[67] 0.09210526 0.10389610 0.11538462 0.12658228 0.01250000 0.02469136
[73] 0.03658537 0.04819277 0.05952381 0.07058824 0.08139535 0.09195402
[79] 0.10227273 0.11235955 0.01111111 0.02197802 0.03260870 0.04301075
[85] 0.05319149 0.06315789 0.07291667 0.08247423 0.09183673 0.10101010
[91] 0.01000000
a*b
Warning in a * b: longer object length is not a multiple of shorter object
length
 [1]  10  22  36  52  70  90 112 136 162 190  20  42  66  92 120 150 182 216 252
[20] 290  30  62  96 132 170 210 252 296 342 390  40  82 126 172 220 270 322 376
[39] 432 490  50 102 156 212 270 330 392 456 522 590  60 122 186 252 320 390 462
[58] 536 612 690  70 142 216 292 370 450 532 616 702 790  80 162 246 332 420 510
[77] 602 696 792 890  90 182 276 372 470 570 672 776 882 990 100
a-b
Warning in a - b: longer object length is not a multiple of shorter object
length
 [1]  -9  -9  -9  -9  -9  -9  -9  -9  -9  -9 -19 -19 -19 -19 -19 -19 -19 -19 -19
[20] -19 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -39 -39 -39 -39 -39 -39 -39 -39
[39] -39 -39 -49 -49 -49 -49 -49 -49 -49 -49 -49 -49 -59 -59 -59 -59 -59 -59 -59
[58] -59 -59 -59 -69 -69 -69 -69 -69 -69 -69 -69 -69 -69 -79 -79 -79 -79 -79 -79
[77] -79 -79 -79 -79 -89 -89 -89 -89 -89 -89 -89 -89 -89 -89 -99
c(10, 100, 100) + 2 # two is added to every element in the vector
[1]  12 102 102

Example for operations between two vectors

v1 <- c(1, 2, 3); v2 <- c(10, 100, 1000)
v1 + v2
[1]   11  102 1003

Add two vectors of unequal length

The short vector repeats until it matches the length of the long vector.

longvec <- seq(10, 100, length=10); shortvec <- c(1, 2, 3, 4, 5)

shortvec + longvec
 [1]  11  22  33  44  55  61  72  83  94 105

2.2 Matrix

Matrix is a 2-dimentional and a homogeneous data structure

Syntax to create a matrix

matrix_name <- matrix(vector_of_elements, 
                      nrow=number_of_rows,
                      ncol=number_of_columns,
                      byrow=logical_value, # If byrow=TRUE, then the matrix is filled in by row.
                      dimnames=list(rnames, cnames)) # To assign row names and columns

Example

matrix(1:6, nrow=2, ncol=3)
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
matrix(1:6, nrow=2)
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
matrix(1:6, ncol=3)
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

2.2.1 Matrix fill by rows/ columns

values <- c(10, 20, 30, 40)
matrix1 <- matrix(values, nrow=2) # Matrix filled by columns (default option)
matrix1
     [,1] [,2]
[1,]   10   30
[2,]   20   40
matrix2 <- matrix(values, nrow=2, byrow=TRUE) # Matrix filled by rows
matrix2
     [,1] [,2]
[1,]   10   20
[2,]   30   40
  • byrow=TRUE: matrix is filled in by row

  • byrow=FALSE: matrix is filled in by column

  • Default is by column

2.2.2 Naming matrix rows and columns

rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
matrix_with_names <- matrix(values, nrow=2, dimnames=list(rnames, cnames))
matrix_with_names
   C1 C2
R1 10 30
R2 20 40

2.2.3 Matrix subscript

matraix_name[i, ] gives the ith row of a matrix

matrix1[1, ]
[1] 10 30

matraix_name[, j] gives the jth column of a matrix

matrix1[, 2]
[1] 30 40

matraix_name[i, j] gives the ith row and jth column element

matrix1[1, 2]
[1] 30
matrix1[1, c(1, 2)] 
[1] 10 30

2.2.4 cbind and rbind

Matrices can be created by column-binding and row-binding with cbind() and rbind()

x <- 1:3
y <- c(10, 100, 1000)
cbind(x, y) # binds matrices horizontally
     x    y
[1,] 1   10
[2,] 2  100
[3,] 3 1000
rbind(x, y) #binds matrices vertically
  [,1] [,2] [,3]
x    1    2    3
y   10  100 1000

2.2.5 Matrix operations

Transpose

t(x)
     [,1] [,2] [,3]
[1,]    1    2    3

Matrix multiplication

y <- matrix(seq(10, 60, by=10), nrow=3)
z <- x %*% y
z
     [,1] [,2]
[1,]  140  320

Find x in: m*x=n

solve(m, n)

2.2.6 Exercise

  1. Write R codes to obtain following matrix outputs
     [,1] [,2] [,3] [,4] [,5]
[1,]   10   30   50   70   90
[2,]   20   40   60   80  100
     [,1] [,2] [,3] [,4] [,5]
[1,]   10   20   30   40   50
[2,]   60   70   80   90  100
     C1 C2 C3 C4
Row1  1  6 11 16
Row2  2  7 12 17
Row3  3  8 13 18
Row4  4  9 14 19
Row5  5 10 15 20
  1. Mr. Perera who lives in Soratha Mawatha - Wijerama wants to sell his house. He wants to decide a price for his house to list it in the market. He believes that the size of the house is one likely determinant of price. He asked from 10 homes in the neighbourhood, “what price should you ask for your home?” and the house size (in square feet). The collected data are shown below:
   size_x price_y
1    1000     810
2    1500    1210
3    2000    1450
4    2500    1610
5    3000    1690
6    3500    2010
7    4000    1490
8    4500    1690
9    5000    1890
10   5500    2410
  1. Write an R code to input size_x and price_y into two separate vectors.

  2. Mr. Perera wants to compute the least squares estimates of the model \(\hat{Y} = \hat{\beta_0} + \hat{\beta_1}X\). Write an R code to compute \(\hat{\beta_0}\) and \(\hat{\beta_1}\) using the matrix operation \(\hat{\beta} = (X^TX)^{-1}X^TY\). Do not use the built-in function lm.

Where,

\(\hat{\beta} =\begin{pmatrix} \hat{\beta_0} \\ \hat{\beta_1} \\ \end{pmatrix}\), \(Y = \begin{pmatrix} y_1 \\ y_2 \\ y_3 \\ . \\ . \\ . \\ y_n \end{pmatrix}\) and \(X = \begin{pmatrix} 1 & x_1 \\ 1 & x_2 \\ 1 & x_3 \\ . \\ . \\ . \\ 1 & x_n \end{pmatrix}\)

2.3 3. Array

  • data structures for storing higher dimensional data.

  • a homogeneous data structure.

  • a special case of the array is the matrix.

array(vector, dimensions, dimnames) #dimnames-as a list
a <-  array(c(10, 20, 30, 40, 50, 60), c(1, 2, 3))
a
, , 1

     [,1] [,2]
[1,]   10   20

, , 2

     [,1] [,2]
[1,]   30   40

, , 3

     [,1] [,2]
[1,]   50   60

2.3.1 Subsetting arrays

a
, , 1

     [,1] [,2]
[1,]   10   20

, , 2

     [,1] [,2]
[1,]   30   40

, , 3

     [,1] [,2]
[1,]   50   60
a[, , 1] # Extract first entry
[1] 10 20
a[1, ,] # All rows in each entry
     [,1] [,2] [,3]
[1,]   10   30   50
[2,]   20   40   60

2.3.2 Exercise

  1. Create the following matrix using the array function
matrix(1:20, ncol=5)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

2.3.3 Array with dimnames

dim1 <- c("A1", "A2"); dim2 <- c("B1", "B2", "B3"); dim3 <- c("c1", "c2", "c3", "c4")
z <- array(1:24, c(2, 3, 4), dimnames = list(dim1, dim2, dim3))
z
, , c1

   B1 B2 B3
A1  1  3  5
A2  2  4  6

, , c2

   B1 B2 B3
A1  7  9 11
A2  8 10 12

, , c3

   B1 B2 B3
A1 13 15 17
A2 14 16 18

, , c4

   B1 B2 B3
A1 19 21 23
A2 20 22 24

2.3.4 Exercise

Create a 3D array with 3 columns, 5 rows, and 2 layers in R, and enter the following values into it:

 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30

2.4 4. Data Frames

  • Rectangular arrangement of data with rows corresponding to observational units and columns corresponding to variables.

  • More general than a matrix in that different columns can contain different modes of data.

  • It’s similar to the datasets you’d typically see in SPSS and MINITAB.

  • Data frames are the most common data structure you’ll deal with in R.

2.4.1 Create a dataframe

Syntax

name_of_the_dataframe <- data.frame(
                          var1_name=vector of values of the first variable,
                          var2_names=vector of values of the second variable)

Example

corona <- data.frame(ID=c("C001", "C002", "C003", "C004"),
                     Location=c("Beijing", "Wuhan", "Shanghai", "Beijing"),
                     Test_Results=c(FALSE, TRUE, FALSE, FALSE))
corona
    ID Location Test_Results
1 C001  Beijing        FALSE
2 C002    Wuhan         TRUE
3 C003 Shanghai        FALSE
4 C004  Beijing        FALSE

To check if it is a dataframe

is.data.frame(corona)
[1] TRUE

2.4.2 Exercise

Write a code to store the following values in a dataframe.

Girth Height Volume
8.3 70 10.3
8.6 65 10.3
8.8 63 10.2
10.5 72 16.4
10.7 81 18.8
10.8 83 19.7
11.0 66 15.6
11.0 75 18.2
11.1 80 22.6
11.2 75 19.9

2.4.3 Some useful functions with dataframes

colnames(corona)
[1] "ID"           "Location"     "Test_Results"
length(corona)
[1] 3
dim(corona)
[1] 4 3
nrow(corona)
[1] 4
ncol(corona)
[1] 3
summary(corona)
      ID              Location         Test_Results   
 Length:4           Length:4           Mode :logical  
 Class :character   Class :character   FALSE:3        
 Mode  :character   Mode  :character   TRUE :1        
str(corona)
'data.frame':   4 obs. of  3 variables:
 $ ID          : chr  "C001" "C002" "C003" "C004"
 $ Location    : chr  "Beijing" "Wuhan" "Shanghai" "Beijing"
 $ Test_Results: logi  FALSE TRUE FALSE FALSE

2.4.4 Convert a matrix to a dataframe

mat <- matrix(1:16, ncol=4)
mat
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
mat_df <- as.data.frame(mat)
mat_df
  V1 V2 V3 V4
1  1  5  9 13
2  2  6 10 14
3  3  7 11 15
4  4  8 12 16

2.4.5 Subsetting data frames

Select rows

head(mat_df) # default it shows 5 rows
  V1 V2 V3 V4
1  1  5  9 13
2  2  6 10 14
3  3  7 11 15
4  4  8 12 16
head(mat_df, 3) # To extract only the first three rows 
  V1 V2 V3 V4
1  1  5  9 13
2  2  6 10 14
3  3  7 11 15
tail(mat_df)
  V1 V2 V3 V4
1  1  5  9 13
2  2  6 10 14
3  3  7 11 15
4  4  8 12 16
mat_df
  V1 V2 V3 V4
1  1  5  9 13
2  2  6 10 14
3  3  7 11 15
4  4  8 12 16

To select some specific rows

mat_df[4, ]
  V1 V2 V3 V4
4  4  8 12 16
index <- c(1, 3)
mat_df[index, ]
  V1 V2 V3 V4
1  1  5  9 13
3  3  7 11 15

Select columns

  1. Select column(s) by variable names
mat_df$V1 # Method 1
[1] 1 2 3 4
mat_df[, "V1"] # Method 2
[1] 1 2 3 4
  1. Select column(s) by index
mat_df[, 2]
[1] 5 6 7 8

2.4.6 Built-in dataframes

there are several built-in data frames (datasets) that you can use directly without needing to import external files.

data(iris)

R comes with many datasets preloaded, mostly from the datasets package. You can see them by running:

data()

2.4.7 Exercise

Question 1

Use the R dataset “iris” to answer the following questions:

  1. How many rows and columns does iris have?

  2. Select the first 4 rows.

  3. Select the last 6 rows.

  4. Select rows 10 to 20, with all columns in the iris dataset.

  5. Select rows 10 to 20 with only the Species, Petal.Width and Petal.Length.

  6. Create a single vector (a new object) called ‘width’ that is the Sepal.Width column of iris.

  7. What are the column names and data types of the different columns in iris?

  8. How many rows in the iris dataset have Petal.Length larger than 5 and Sepal.Width smaller than 3?

Question 2

This exercise is based on mtcars built-in-dataset in R. Write R codes to obtain the answers for the followings.

  1. To obtain the help file of mtcars
  1. How many cars are in the mtcars dataset?
  1. How many variables are in the mtcars dataset?
  1. What are the column names of the mtcars dataset?
  1. What is the mean miles per gallon (mpg) of the cars in the dataset?
  1. Which car has the highest horsepower (hp)?
  1. What is the mean weight (wt) of the cars in the dataset?
  1. How many cars have 8 cylinders (cyl)?
  1. What is the range of displacement (disp) values in the dataset?
  1. What is the median quarter mile time (qsec) for the cars?
  1. How many cars have a manual transmission (am = 1)?
  1. What is the maximum miles per gallon (mpg) in the dataset?
  1. What is the minimum horsepower (hp) recorded in the dataset?
  1. Which car has the lowest weight (wt)?
  1. How many cars have 4 gears (gear)?
  1. What is the standard deviation of the mpg variable?
  1. What is the total number of carburetors (carb) for all cars combined?
  1. How many cars have a quarter mile time (qsec) less than 18 seconds?
  1. What is the mean value of the gear variable for cars with 6 cylinders (cyl)?
  1. How many cars have more than 100 horsepower (hp)?
  1. What is the correlation between horsepower (hp) and miles per gallon (mpg)?

2.5 5. List

  • Lists are heterogeneous

Syntax

list_name <- list(entry1, entry2, entry3, ...)

Example

first_list <-list(1:3, matrix(1:6, nrow=2), iris)
first_list
[[1]]
[1] 1 2 3

[[2]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[[3]]
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
6            5.4         3.9          1.7         0.4     setosa
7            4.6         3.4          1.4         0.3     setosa
8            5.0         3.4          1.5         0.2     setosa
9            4.4         2.9          1.4         0.2     setosa
10           4.9         3.1          1.5         0.1     setosa
11           5.4         3.7          1.5         0.2     setosa
12           4.8         3.4          1.6         0.2     setosa
13           4.8         3.0          1.4         0.1     setosa
14           4.3         3.0          1.1         0.1     setosa
15           5.8         4.0          1.2         0.2     setosa
16           5.7         4.4          1.5         0.4     setosa
17           5.4         3.9          1.3         0.4     setosa
18           5.1         3.5          1.4         0.3     setosa
19           5.7         3.8          1.7         0.3     setosa
20           5.1         3.8          1.5         0.3     setosa
21           5.4         3.4          1.7         0.2     setosa
22           5.1         3.7          1.5         0.4     setosa
23           4.6         3.6          1.0         0.2     setosa
24           5.1         3.3          1.7         0.5     setosa
25           4.8         3.4          1.9         0.2     setosa
26           5.0         3.0          1.6         0.2     setosa
27           5.0         3.4          1.6         0.4     setosa
28           5.2         3.5          1.5         0.2     setosa
29           5.2         3.4          1.4         0.2     setosa
30           4.7         3.2          1.6         0.2     setosa
31           4.8         3.1          1.6         0.2     setosa
32           5.4         3.4          1.5         0.4     setosa
33           5.2         4.1          1.5         0.1     setosa
34           5.5         4.2          1.4         0.2     setosa
35           4.9         3.1          1.5         0.2     setosa
36           5.0         3.2          1.2         0.2     setosa
37           5.5         3.5          1.3         0.2     setosa
38           4.9         3.6          1.4         0.1     setosa
39           4.4         3.0          1.3         0.2     setosa
40           5.1         3.4          1.5         0.2     setosa
41           5.0         3.5          1.3         0.3     setosa
42           4.5         2.3          1.3         0.3     setosa
43           4.4         3.2          1.3         0.2     setosa
44           5.0         3.5          1.6         0.6     setosa
45           5.1         3.8          1.9         0.4     setosa
46           4.8         3.0          1.4         0.3     setosa
47           5.1         3.8          1.6         0.2     setosa
48           4.6         3.2          1.4         0.2     setosa
49           5.3         3.7          1.5         0.2     setosa
50           5.0         3.3          1.4         0.2     setosa
51           7.0         3.2          4.7         1.4 versicolor
52           6.4         3.2          4.5         1.5 versicolor
53           6.9         3.1          4.9         1.5 versicolor
54           5.5         2.3          4.0         1.3 versicolor
55           6.5         2.8          4.6         1.5 versicolor
56           5.7         2.8          4.5         1.3 versicolor
57           6.3         3.3          4.7         1.6 versicolor
58           4.9         2.4          3.3         1.0 versicolor
59           6.6         2.9          4.6         1.3 versicolor
60           5.2         2.7          3.9         1.4 versicolor
61           5.0         2.0          3.5         1.0 versicolor
62           5.9         3.0          4.2         1.5 versicolor
63           6.0         2.2          4.0         1.0 versicolor
64           6.1         2.9          4.7         1.4 versicolor
65           5.6         2.9          3.6         1.3 versicolor
66           6.7         3.1          4.4         1.4 versicolor
67           5.6         3.0          4.5         1.5 versicolor
68           5.8         2.7          4.1         1.0 versicolor
69           6.2         2.2          4.5         1.5 versicolor
70           5.6         2.5          3.9         1.1 versicolor
71           5.9         3.2          4.8         1.8 versicolor
72           6.1         2.8          4.0         1.3 versicolor
73           6.3         2.5          4.9         1.5 versicolor
74           6.1         2.8          4.7         1.2 versicolor
75           6.4         2.9          4.3         1.3 versicolor
76           6.6         3.0          4.4         1.4 versicolor
77           6.8         2.8          4.8         1.4 versicolor
78           6.7         3.0          5.0         1.7 versicolor
79           6.0         2.9          4.5         1.5 versicolor
80           5.7         2.6          3.5         1.0 versicolor
81           5.5         2.4          3.8         1.1 versicolor
82           5.5         2.4          3.7         1.0 versicolor
83           5.8         2.7          3.9         1.2 versicolor
84           6.0         2.7          5.1         1.6 versicolor
85           5.4         3.0          4.5         1.5 versicolor
86           6.0         3.4          4.5         1.6 versicolor
87           6.7         3.1          4.7         1.5 versicolor
88           6.3         2.3          4.4         1.3 versicolor
89           5.6         3.0          4.1         1.3 versicolor
90           5.5         2.5          4.0         1.3 versicolor
91           5.5         2.6          4.4         1.2 versicolor
92           6.1         3.0          4.6         1.4 versicolor
93           5.8         2.6          4.0         1.2 versicolor
94           5.0         2.3          3.3         1.0 versicolor
95           5.6         2.7          4.2         1.3 versicolor
96           5.7         3.0          4.2         1.2 versicolor
97           5.7         2.9          4.2         1.3 versicolor
98           6.2         2.9          4.3         1.3 versicolor
99           5.1         2.5          3.0         1.1 versicolor
100          5.7         2.8          4.1         1.3 versicolor
101          6.3         3.3          6.0         2.5  virginica
102          5.8         2.7          5.1         1.9  virginica
103          7.1         3.0          5.9         2.1  virginica
104          6.3         2.9          5.6         1.8  virginica
105          6.5         3.0          5.8         2.2  virginica
106          7.6         3.0          6.6         2.1  virginica
107          4.9         2.5          4.5         1.7  virginica
108          7.3         2.9          6.3         1.8  virginica
109          6.7         2.5          5.8         1.8  virginica
110          7.2         3.6          6.1         2.5  virginica
111          6.5         3.2          5.1         2.0  virginica
112          6.4         2.7          5.3         1.9  virginica
113          6.8         3.0          5.5         2.1  virginica
114          5.7         2.5          5.0         2.0  virginica
115          5.8         2.8          5.1         2.4  virginica
116          6.4         3.2          5.3         2.3  virginica
117          6.5         3.0          5.5         1.8  virginica
118          7.7         3.8          6.7         2.2  virginica
119          7.7         2.6          6.9         2.3  virginica
120          6.0         2.2          5.0         1.5  virginica
121          6.9         3.2          5.7         2.3  virginica
122          5.6         2.8          4.9         2.0  virginica
123          7.7         2.8          6.7         2.0  virginica
124          6.3         2.7          4.9         1.8  virginica
125          6.7         3.3          5.7         2.1  virginica
126          7.2         3.2          6.0         1.8  virginica
127          6.2         2.8          4.8         1.8  virginica
128          6.1         3.0          4.9         1.8  virginica
129          6.4         2.8          5.6         2.1  virginica
130          7.2         3.0          5.8         1.6  virginica
131          7.4         2.8          6.1         1.9  virginica
132          7.9         3.8          6.4         2.0  virginica
133          6.4         2.8          5.6         2.2  virginica
134          6.3         2.8          5.1         1.5  virginica
135          6.1         2.6          5.6         1.4  virginica
136          7.7         3.0          6.1         2.3  virginica
137          6.3         3.4          5.6         2.4  virginica
138          6.4         3.1          5.5         1.8  virginica
139          6.0         3.0          4.8         1.8  virginica
140          6.9         3.1          5.4         2.1  virginica
141          6.7         3.1          5.6         2.4  virginica
142          6.9         3.1          5.1         2.3  virginica
143          5.8         2.7          5.1         1.9  virginica
144          6.8         3.2          5.9         2.3  virginica
145          6.7         3.3          5.7         2.5  virginica
146          6.7         3.0          5.2         2.3  virginica
147          6.3         2.5          5.0         1.9  virginica
148          6.5         3.0          5.2         2.0  virginica
149          6.2         3.4          5.4         2.3  virginica
150          5.9         3.0          5.1         1.8  virginica

2.5.1 Structure of a list

str(first_list)
List of 3
 $ : int [1:3] 1 2 3
 $ : int [1:2, 1:3] 1 2 3 4 5 6
 $ :'data.frame':   150 obs. of  5 variables:
  ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

2.5.2 Extract elements from a list

first_list[[1]]
[1] 1 2 3
first_list[[3]]$Species
  [1] setosa     setosa     setosa     setosa     setosa     setosa    
  [7] setosa     setosa     setosa     setosa     setosa     setosa    
 [13] setosa     setosa     setosa     setosa     setosa     setosa    
 [19] setosa     setosa     setosa     setosa     setosa     setosa    
 [25] setosa     setosa     setosa     setosa     setosa     setosa    
 [31] setosa     setosa     setosa     setosa     setosa     setosa    
 [37] setosa     setosa     setosa     setosa     setosa     setosa    
 [43] setosa     setosa     setosa     setosa     setosa     setosa    
 [49] setosa     setosa     versicolor versicolor versicolor versicolor
 [55] versicolor versicolor versicolor versicolor versicolor versicolor
 [61] versicolor versicolor versicolor versicolor versicolor versicolor
 [67] versicolor versicolor versicolor versicolor versicolor versicolor
 [73] versicolor versicolor versicolor versicolor versicolor versicolor
 [79] versicolor versicolor versicolor versicolor versicolor versicolor
 [85] versicolor versicolor versicolor versicolor versicolor versicolor
 [91] versicolor versicolor versicolor versicolor versicolor versicolor
 [97] versicolor versicolor versicolor versicolor virginica  virginica 
[103] virginica  virginica  virginica  virginica  virginica  virginica 
[109] virginica  virginica  virginica  virginica  virginica  virginica 
[115] virginica  virginica  virginica  virginica  virginica  virginica 
[121] virginica  virginica  virginica  virginica  virginica  virginica 
[127] virginica  virginica  virginica  virginica  virginica  virginica 
[133] virginica  virginica  virginica  virginica  virginica  virginica 
[139] virginica  virginica  virginica  virginica  virginica  virginica 
[145] virginica  virginica  virginica  virginica  virginica  virginica 
Levels: setosa versicolor virginica

2.5.3 Name entries in a list

first_list_with_names <-list(a=1:3, b=matrix(1:6, nrow=2), c=iris)
first_list_with_names
$a
[1] 1 2 3

$b
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

$c
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
6            5.4         3.9          1.7         0.4     setosa
7            4.6         3.4          1.4         0.3     setosa
8            5.0         3.4          1.5         0.2     setosa
9            4.4         2.9          1.4         0.2     setosa
10           4.9         3.1          1.5         0.1     setosa
11           5.4         3.7          1.5         0.2     setosa
12           4.8         3.4          1.6         0.2     setosa
13           4.8         3.0          1.4         0.1     setosa
14           4.3         3.0          1.1         0.1     setosa
15           5.8         4.0          1.2         0.2     setosa
16           5.7         4.4          1.5         0.4     setosa
17           5.4         3.9          1.3         0.4     setosa
18           5.1         3.5          1.4         0.3     setosa
19           5.7         3.8          1.7         0.3     setosa
20           5.1         3.8          1.5         0.3     setosa
21           5.4         3.4          1.7         0.2     setosa
22           5.1         3.7          1.5         0.4     setosa
23           4.6         3.6          1.0         0.2     setosa
24           5.1         3.3          1.7         0.5     setosa
25           4.8         3.4          1.9         0.2     setosa
26           5.0         3.0          1.6         0.2     setosa
27           5.0         3.4          1.6         0.4     setosa
28           5.2         3.5          1.5         0.2     setosa
29           5.2         3.4          1.4         0.2     setosa
30           4.7         3.2          1.6         0.2     setosa
31           4.8         3.1          1.6         0.2     setosa
32           5.4         3.4          1.5         0.4     setosa
33           5.2         4.1          1.5         0.1     setosa
34           5.5         4.2          1.4         0.2     setosa
35           4.9         3.1          1.5         0.2     setosa
36           5.0         3.2          1.2         0.2     setosa
37           5.5         3.5          1.3         0.2     setosa
38           4.9         3.6          1.4         0.1     setosa
39           4.4         3.0          1.3         0.2     setosa
40           5.1         3.4          1.5         0.2     setosa
41           5.0         3.5          1.3         0.3     setosa
42           4.5         2.3          1.3         0.3     setosa
43           4.4         3.2          1.3         0.2     setosa
44           5.0         3.5          1.6         0.6     setosa
45           5.1         3.8          1.9         0.4     setosa
46           4.8         3.0          1.4         0.3     setosa
47           5.1         3.8          1.6         0.2     setosa
48           4.6         3.2          1.4         0.2     setosa
49           5.3         3.7          1.5         0.2     setosa
50           5.0         3.3          1.4         0.2     setosa
51           7.0         3.2          4.7         1.4 versicolor
52           6.4         3.2          4.5         1.5 versicolor
53           6.9         3.1          4.9         1.5 versicolor
54           5.5         2.3          4.0         1.3 versicolor
55           6.5         2.8          4.6         1.5 versicolor
56           5.7         2.8          4.5         1.3 versicolor
57           6.3         3.3          4.7         1.6 versicolor
58           4.9         2.4          3.3         1.0 versicolor
59           6.6         2.9          4.6         1.3 versicolor
60           5.2         2.7          3.9         1.4 versicolor
61           5.0         2.0          3.5         1.0 versicolor
62           5.9         3.0          4.2         1.5 versicolor
63           6.0         2.2          4.0         1.0 versicolor
64           6.1         2.9          4.7         1.4 versicolor
65           5.6         2.9          3.6         1.3 versicolor
66           6.7         3.1          4.4         1.4 versicolor
67           5.6         3.0          4.5         1.5 versicolor
68           5.8         2.7          4.1         1.0 versicolor
69           6.2         2.2          4.5         1.5 versicolor
70           5.6         2.5          3.9         1.1 versicolor
71           5.9         3.2          4.8         1.8 versicolor
72           6.1         2.8          4.0         1.3 versicolor
73           6.3         2.5          4.9         1.5 versicolor
74           6.1         2.8          4.7         1.2 versicolor
75           6.4         2.9          4.3         1.3 versicolor
76           6.6         3.0          4.4         1.4 versicolor
77           6.8         2.8          4.8         1.4 versicolor
78           6.7         3.0          5.0         1.7 versicolor
79           6.0         2.9          4.5         1.5 versicolor
80           5.7         2.6          3.5         1.0 versicolor
81           5.5         2.4          3.8         1.1 versicolor
82           5.5         2.4          3.7         1.0 versicolor
83           5.8         2.7          3.9         1.2 versicolor
84           6.0         2.7          5.1         1.6 versicolor
85           5.4         3.0          4.5         1.5 versicolor
86           6.0         3.4          4.5         1.6 versicolor
87           6.7         3.1          4.7         1.5 versicolor
88           6.3         2.3          4.4         1.3 versicolor
89           5.6         3.0          4.1         1.3 versicolor
90           5.5         2.5          4.0         1.3 versicolor
91           5.5         2.6          4.4         1.2 versicolor
92           6.1         3.0          4.6         1.4 versicolor
93           5.8         2.6          4.0         1.2 versicolor
94           5.0         2.3          3.3         1.0 versicolor
95           5.6         2.7          4.2         1.3 versicolor
96           5.7         3.0          4.2         1.2 versicolor
97           5.7         2.9          4.2         1.3 versicolor
98           6.2         2.9          4.3         1.3 versicolor
99           5.1         2.5          3.0         1.1 versicolor
100          5.7         2.8          4.1         1.3 versicolor
101          6.3         3.3          6.0         2.5  virginica
102          5.8         2.7          5.1         1.9  virginica
103          7.1         3.0          5.9         2.1  virginica
104          6.3         2.9          5.6         1.8  virginica
105          6.5         3.0          5.8         2.2  virginica
106          7.6         3.0          6.6         2.1  virginica
107          4.9         2.5          4.5         1.7  virginica
108          7.3         2.9          6.3         1.8  virginica
109          6.7         2.5          5.8         1.8  virginica
110          7.2         3.6          6.1         2.5  virginica
111          6.5         3.2          5.1         2.0  virginica
112          6.4         2.7          5.3         1.9  virginica
113          6.8         3.0          5.5         2.1  virginica
114          5.7         2.5          5.0         2.0  virginica
115          5.8         2.8          5.1         2.4  virginica
116          6.4         3.2          5.3         2.3  virginica
117          6.5         3.0          5.5         1.8  virginica
118          7.7         3.8          6.7         2.2  virginica
119          7.7         2.6          6.9         2.3  virginica
120          6.0         2.2          5.0         1.5  virginica
121          6.9         3.2          5.7         2.3  virginica
122          5.6         2.8          4.9         2.0  virginica
123          7.7         2.8          6.7         2.0  virginica
124          6.3         2.7          4.9         1.8  virginica
125          6.7         3.3          5.7         2.1  virginica
126          7.2         3.2          6.0         1.8  virginica
127          6.2         2.8          4.8         1.8  virginica
128          6.1         3.0          4.9         1.8  virginica
129          6.4         2.8          5.6         2.1  virginica
130          7.2         3.0          5.8         1.6  virginica
131          7.4         2.8          6.1         1.9  virginica
132          7.9         3.8          6.4         2.0  virginica
133          6.4         2.8          5.6         2.2  virginica
134          6.3         2.8          5.1         1.5  virginica
135          6.1         2.6          5.6         1.4  virginica
136          7.7         3.0          6.1         2.3  virginica
137          6.3         3.4          5.6         2.4  virginica
138          6.4         3.1          5.5         1.8  virginica
139          6.0         3.0          4.8         1.8  virginica
140          6.9         3.1          5.4         2.1  virginica
141          6.7         3.1          5.6         2.4  virginica
142          6.9         3.1          5.1         2.3  virginica
143          5.8         2.7          5.1         1.9  virginica
144          6.8         3.2          5.9         2.3  virginica
145          6.7         3.3          5.7         2.5  virginica
146          6.7         3.0          5.2         2.3  virginica
147          6.3         2.5          5.0         1.9  virginica
148          6.5         3.0          5.2         2.0  virginica
149          6.2         3.4          5.4         2.3  virginica
150          5.9         3.0          5.1         1.8  virginica

2.5.4 Extract elements using names

str(first_list_with_names)
List of 3
 $ a: int [1:3] 1 2 3
 $ b: int [1:2, 1:3] 1 2 3 4 5 6
 $ c:'data.frame':  150 obs. of  5 variables:
  ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
first_list_with_names$a
[1] 1 2 3
first_list_with_names$c$Species
  [1] setosa     setosa     setosa     setosa     setosa     setosa    
  [7] setosa     setosa     setosa     setosa     setosa     setosa    
 [13] setosa     setosa     setosa     setosa     setosa     setosa    
 [19] setosa     setosa     setosa     setosa     setosa     setosa    
 [25] setosa     setosa     setosa     setosa     setosa     setosa    
 [31] setosa     setosa     setosa     setosa     setosa     setosa    
 [37] setosa     setosa     setosa     setosa     setosa     setosa    
 [43] setosa     setosa     setosa     setosa     setosa     setosa    
 [49] setosa     setosa     versicolor versicolor versicolor versicolor
 [55] versicolor versicolor versicolor versicolor versicolor versicolor
 [61] versicolor versicolor versicolor versicolor versicolor versicolor
 [67] versicolor versicolor versicolor versicolor versicolor versicolor
 [73] versicolor versicolor versicolor versicolor versicolor versicolor
 [79] versicolor versicolor versicolor versicolor versicolor versicolor
 [85] versicolor versicolor versicolor versicolor versicolor versicolor
 [91] versicolor versicolor versicolor versicolor versicolor versicolor
 [97] versicolor versicolor versicolor versicolor virginica  virginica 
[103] virginica  virginica  virginica  virginica  virginica  virginica 
[109] virginica  virginica  virginica  virginica  virginica  virginica 
[115] virginica  virginica  virginica  virginica  virginica  virginica 
[121] virginica  virginica  virginica  virginica  virginica  virginica 
[127] virginica  virginica  virginica  virginica  virginica  virginica 
[133] virginica  virginica  virginica  virginica  virginica  virginica 
[139] virginica  virginica  virginica  virginica  virginica  virginica 
[145] virginica  virginica  virginica  virginica  virginica  virginica 
Levels: setosa versicolor virginica

2.5.5 Exercise

c("Jan","Feb","Mar")
[1] "Jan" "Feb" "Mar"
matrix(c(3,9,5,1,-2,8), nrow = 2)
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8
list("green",12.3)
[[1]]
[1] "green"

[[2]]
[1] 12.3
  1. Create a list containing the above vector, matrix and the list.

  2. Name the elements as first, second and third.