LECTURE 4 Matrices and Arrays

Page 1: Lecture Introduction

  • Lecture 4 Outline: Matrices, Arrays, Lists

Page 2: Review of Data Structures

  • Data Structures Covered:

    • Scalars

    • Vectors

    • Matrices

    • Arrays

    • Lists

    • Data Frames

Page 3: Understanding Matrices

  • Definition:

    • Matrices are R objects with elements arranged in a two-dimensional rectangular layout.

    • All elements must be of the same type.

  • Creation:

    • Use matrix() function to create a matrix.

Page 4: Matrix Syntax

  • Function Syntax: matrix(data, nrow, ncol, byrow, dimnames)

    • data: Input vector for matrix elements.

    • nrow: Number of rows for the matrix.

    • ncol: Number of columns for the matrix.

    • byrow: Logical value; if TRUE, fills by row, defaults to FALSE (fills by column).

    • dimnames: Names assigned to rows and columns (optional).

Page 5: Matrix Operations

  • Practical Demonstration: a) Create a 5 x 4 numeric matrix with elements 1 to 20. b) Select the 3rd column of the matrix. c) Select rows 2, 3, 4 of columns 1, 2, 3. d) Determine the dimensions of the matrix.

Page 6: R Matrices for Statistical Analysis

  • Steps:

    1. Construct a matrix using provided data in RStudio, filling by row.

    2. Add appropriate row and column names.

    3. Analyze data: Count people with blond hair and blue eyes.

    4. Perform sum operations on row and column values.

Page 7: Matrix Modification Techniques

  • Column Binding: Use cbind() to create a matrix with vector values as columns.

  • Row Binding: Use rbind() to create a matrix with vector values as rows.

  • Removing Elements: Use subset() function.

  • Overwriting Elements: Use [ ] notation for specific elements.

Page 8: Creating and Modifying Matrices

  • Practical Tasks:

    • Create vectors a, b, d.

    • Combine a, b, d into matrix CC as columns and matrix CR as rows.

    • Remove column 'a' from matrix CC.

    • Replace the first row of matrix CC with sequence 1:2.

    • Replace the second column of matrix CR with sequence 4:6.

Page 9: Understanding Arrays

  • Definition:

    • Arrays are matrices in higher dimensions.

    • Comprised of 2 or more matrices.

    • Accept vectors as input.

Page 10: Array Creation

  • Creation Function: array()

    • Syntax: array(data=NA, dim=length(data), dimnames=NULL)

      • data: Values used to fill the array.

      • dim: Dimensions of the array defined by the length of the data.

      • dimnames: Optional list of names for the dimensions.

Page 11: Practical Array Tasks

  • Tasks:

    1. Create an array of two 3x3 matrices.

    2. Modify the matrix to include:

      • a) Column names

      • b) Row names

      • c) Matrix names.

Page 12: Calculations Across Arrays

  • Syntax for Function Application:

    • apply(x, margin, fun)

      • x: The array.

      • margin: Indicates rows/columns for function application (1 for rows, 2 for columns, c(1, 2) for both).

      • fun: The function to apply across the array.

Page 13: Array Calculations Demonstration

  • Vectors:

    • aaa <- c(2,3,4,6)

    • bbb <- c(5,6,10,12,45)

    • Instructions:

      1. Create an array with two 3x4 matrices from aaa and bbb.

      2. Sum the rows.

      3. Sum the columns.

      4. Sum the individual cells.

Page 14: Additional Matrix Insights

  • Points of Interest:

    • Code examples shown in lecture.

    • Methods to create integer/numeric vectors.

    • Converting vectors to matrices.

    • Changing column and row names.

    • byrow option/argument explanations.

Page 15: Introduction to Lists

  • Definition:

    • A list in R can contain objects of any type, including other lists.

    • Useful for statistical tests that return lists.

    • Created using the list() function.

    • Lists help organize and manipulate data efficiently.

Page 16: Working with Lists

  • Key Topics:

    • Creating a list.

    • Accessing elements within a list.

    • Adding new elements to a list.

Page 17: Summary

  • Covered data structures:

    • Matrices

    • Arrays

    • Lists.

Page 18: Next Lecture Preview

  • Topics: Logical Values and Factors.

  • Focus: Applied Statistical Computing and Graphics.