Matrices
- Make a 5x3 matrix (5 rows, 3 columns) using
matrix( )
.
Ex: matrix(1:6,nrow=3,ncol=2)
makes a 3x2 matrix using
numbers between 1 and 6.
- What happens when you use
byrow = TRUE
in your
matrix( )
as an additional argument? Assign the matrix to a
variable m
.
- Extract the first 2 columns and first 3 rows of your matrix using
[ ]
notation.
- Extract the last two rows of the matrix you created previously.
- Multiplication table: write an R code to print out the 9x9
multiplication table.
- Create a vector
x
containing numbers from 1 to 15 and a
vector y
containing numbers from 24 to 35. Can you combine
these vectors into a matrix? Explain the result.
- Create a new matrix called
M
by selecting all rows
except the last 3 and all the columns from the previous matrix. Evaluate
the numbers of rows and columns of the matrix.
- Assign rownames and colnames to
M
.
- Create a logical matrix (mix TRUE and FALSE values)
L
of same dimensions as M
.
Sum L
and M
. What happened? Comment the
result.
Finally, multiply L
and M
and explain the
result.
- Perform the algebric matrix multiplication of
L
and
M
- Generate different vectors:
- 13 random numbers with mean = 6, sd = 3
- 13 random numbers uniformly distributed between -30 and 140
- a logical vector identifying the numbers in the first vector that
are > 4.5
- a logical vector identifying the numbers in the first vector that
are, in absolute value, < 8
- a logical vector identifying the numbers in the second vector that
are, in absolute value, > 20 and negative
- a vector containing the sum between the first vector and the second
vector
- a vector containing the multiplication between the third vector and
the fourth vector
Create a matrix binding all the vectors by row, evaluate the
dimensions, traspose it and evaluate the dimensions again.
- Create a numeric 5x5 matrix, containing numbers from 1 to 5.
Transform the matrix in a upper triangular matrix by assigning to the
lower triangle
NA
values. Then:
- Extract the values on the diagonal
- Extract the value in the third column and second row.
- Extract values in the fourth column.
- Extract values in the fifth row and assign them to a vector
a
.
- Print the positions of the vector
a
in which the number
5 is found.
- Create a logical vector
b
by testing whether the values
in a
are contained in c(1,3)
- Use the vector
b
to extract from the fifth column of
the matrix the values where b
is TRUE