3. Working with Matrices Flashcards

1
Q

Naming matrix rows and columns

A

Assign name attributes to rows of a matrix:

rownames(matrix)

Assign name attributes to columns of a matrix:

colnames(matrix)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Matrix operations

A

Finding Matrix Dimensions

  • dim(math_chemistry)

Combining Vectors or Matrices by Row

  • rbind(matrix_1, matrix_2)
  • rbind(vector_1, vector_2)
  • rbind(vector_1, matrix_1)

Combining Vectors or Matrices by Column

  • cbind(matrix_1, matrix_2)
  • cbind(vector_1, vector_2)
  • cbind(vector_1, matrix_1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Indexing matrices by element

A

Extract a single element:

  • matrix[2,5]
  • matrix[“Stanford”,”patents”]

Extract multiple elements:

  • matrix[c(1,2),c(1,3)]
  • matrix[c(“Harvard”,”Stanford”),c(“world_rank”,”influence”)]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Indexing matrices by rows and columns

A

Extract a single row:

  • matrix[1,]
  • matrix[“Harvard”,]

Extract a single column:

  • matrix[,2]
  • matrix[,”quality_of_education”]

Extract multiple rows or columns:

  • matrix[,c(“quality_of_education”,”influence”,”broad_impact”)]
  • matrix[,c(“2,3,4”)]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Rank values of a vector

A

Rank values of a vector:

  • rank(vector)

Rank values of a matrix:

  • rank(matrix[,”column”])
  • rank(matrix[“row”,])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Calculate the sum of values in a vector or a matrix

A

Sum of values in a vector:

  • sum(vector)

Sum of values in a matrix:

  • sum(matrix[,”column”])
  • sum(matrix[“row”,])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Concepts

A

Like vectors, matrices only contain one data type. Unlike vectors, they are two-dimensional.

When adding a vector to a matrix, it’s good practice to make sure the new vector is the same length as the number of rows or columns in the matrix.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly