vectors
factors
lists
matrices
arrays
dataframes
Lists are flexible structures that can contain objects of different types and different sizes (each element can itself be a list), and they are created with the list() command
List develop in depth…
We can assign names to the elements of a list…
We can assign names to the elements of a list…
and access them by calling them by “name” with the $ operator
Like vectors, lists also have a length (length()) and possibly names (names()).
The str() (structure) command is very useful for lists because it gives an overview of their structure:
The difference between the square brackets is whether we want to subset the list, getting another list, or directly access the inner element:
If we want to actually subset the list, we must always use single square brackets:
Lists have a one-dimensional structure that can develop in depth. To select nested elements we can chain several brackets:
They are flexible and allow us to store/organize complex and varied data/information. For example, the lm() function returns a list containing various pieces of information and results about the tested model.
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.74636 -0.72466 -0.04939 0.77853 2.03866
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.09571 0.19741 0.485 0.632
x 0.01237 0.17956 0.069 0.946
Residual standard error: 1.02 on 28 degrees of freedom
Multiple R-squared: 0.0001694, Adjusted R-squared: -0.03554
F-statistic: 0.004744 on 1 and 28 DF, p-value: 0.9456
List of 12
$ coefficients : Named num [1:2] 0.0957 0.0124
..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
$ residuals : Named num [1:30] 0.913 1.287 1.69 -0.475 -1.746 ...
..- attr(*, "names")= chr [1:30] "1" "2" "3" "4" ...
$ effects : Named num [1:30] -0.549 0.0702 1.889 -0.8189 -1.7025 ...
..- attr(*, "names")= chr [1:30] "(Intercept)" "x" "" "" ...
$ rank : int 2
$ fitted.values: Named num [1:30] 0.1114 0.0859 0.1275 0.0867 0.1159 ...
..- attr(*, "names")= chr [1:30] "1" "2" "3" "4" ...
$ assign : int [1:2] 0 1
$ qr :List of 5
..$ qr : num [1:30, 1:2] -5.477 0.183 0.183 0.183 0.183 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:30] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "(Intercept)" "x"
.. ..- attr(*, "assign")= int [1:2] 0 1
..$ qraux: num [1:2] 1.18 1.23
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..$ rank : int 2
..- attr(*, "class")= chr "qr"
$ df.residual : int 28
$ xlevels : Named list()
$ call : language lm(formula = y ~ x)
$ terms :Classes 'terms', 'formula' language y ~ x
.. ..- attr(*, "variables")= language list(y, x)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. ..$ : chr "x"
.. ..- attr(*, "term.labels")= chr "x"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(y, x)
.. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. ..- attr(*, "names")= chr [1:2] "y" "x"
$ model :'data.frame': 30 obs. of 2 variables:
..$ y: num [1:30] 1.024 1.373 1.818 -0.389 -1.631 ...
..$ x: num [1:30] 1.265 -0.79 2.569 -0.729 1.629 ...
..- attr(*, "terms")=Classes 'terms', 'formula' language y ~ x
.. .. ..- attr(*, "variables")= language list(y, x)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. .. ..$ : chr "x"
.. .. ..- attr(*, "term.labels")= chr "x"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(y, x)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
- attr(*, "class")= chr "lm"
vectors
factors
lists
matrices
arrays
dataframes
Matrices are a two-dimensional data structure (characterized by 2 dimensions, dim()), where the number of rows is dimension 1 and the number of columns is dimension 2.
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
They can contain only one type of data
Since they are two-dimensional, we need two position indices (rows and columns) to identify an element
They can be seen as a set of single vectors
The number of rows and columns does not have to be the same (square matrix), but the size of the matrix must be compatible with the data vector:
What does R do by default?
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 2 1 2
[3,] 1 2 1
warnings: the function informs us about something potentially problematic, but (more or less!!) everything is fine
To identify one or more elements of a matrix we need row and/or column indices separated by a comma, again inside square brackets: matrix[row, column]
We can also select an entire row or column
As with vectors, here too we can apply logical indexing:
Vectors are created with the c() function and can be concatenated using the same function:
Matrices can be joined together with the commands cbind() or rbind()
cbind()
rbind()
Do you notice anything “strange”?
As with vectors, mathematical operations can also be applied to matrices:
vectors
factors
lists
matrices
arrays
dataframes
Arrays are n-dimensional objects (e.g., if a matrix is a square, an array is a cube). The same properties of matrices apply, scaled to n dimensions; they are created with the array command
Indexing works the same way as with matrices, adding one dimension
Open this link and keep it open: