Extending Vectors

Matrices

While you’re waiting…

In your head, list the three Cal students you like the most. Do the same for the three Cal students you like the least.

Change in place syntax

You can assign into a subsetted vector to change its contents in place.

z <- c(apple = 2, banana = 4, cherry = 8)
z[c(TRUE, FALSE, TRUE)] <- 12
z
 apple banana cherry 
    12      4     12 
names(z)
[1] "apple"  "banana" "cherry"
names(z) <- c("ali", "bob", "chip")
z
 ali  bob chip 
  12    4   12 

Question

w <- c(FALSE, FALSE, TRUE, TRUE, FALSE)

Write down two ways to change-in-place the FALSE to TRUE, each using a different form of subsetting.

01:30

Announcements

  • Quiz Thursday
  • PS 2 due tomorrow at 11:59 pm
  • Solutions posted immediately afterwards

Review Vectors

  • Central data structure in R
    • Vectorization
    • Vector Recycling
  • Ordered set of elements
    • Subsetting with []
  • Atomic: elements of the same type
    • Type coersion

How we got here

card_108
[1] "Golden Klaxons" "3000"          
distances <- as.double(card_108[2])
distances
[1] 3000

Sampson’s Monks

Franklin Sampson, Sociology PhD

Cornell University

St. Anthony’s Monastery, New England

F. S. Sampson. A novitiate in a period of change: An experimental and case study of social relationships. PhD thesis, Cornell University. 1968.

F. S. Sampson. A novitiate in a period of change: An experimental and case study of social relationships. PhD thesis, Cornell University. 1968.

While you’re waiting…

In your head, list the three Cal students you like the most. Do the same for the three Cal students you like the least.

Sampson’s Questionnaire

Question: With a neighbor discuss two data structures that could store all 18 responses in one object (it need not be an atomic vector).

02:30

Matrices

Matrix

A two-dimensional data structure for atomic data. Created with matrix().

mat1 <- matrix(data = c(1, 2, 3, 4), nrow = 2)
mat1
     [,1] [,2]
[1,]    1    3
[2,]    2    4
typeof(mat1)
[1] "double"

Note: defaults to fill the matrix by columns (byrow = FALSE)

Matrices as Vectors

vec <- 1:4
dim(vec) <- c(2, 2)
vec
     [,1] [,2]
[1,]    1    3
[2,]    2    4

Matrices are simply vectors with dimensions dim(). All vector behavior still applies.

Question

Using what you know about how vectors work, predict the outcome of the following:

matrix(c(1, 2, 3, 4), nrow = 2) *
    matrix(c(1, 2, 3, 4), nrow = 2)
matrix(data = c("iz", "buzz", "poppy"), nrow = 2)
matrix(c(TRUE, 3L, 1.0, FALSE), nrow = 2)
02:00

Matrix Subsetting

By Index

mat <- matrix(1:4, nrow = 2)
mat
     [,1] [,2]
[1,]    1    3
[2,]    2    4
mat[1, 2]
[1] 3

Notes:

  • subsetting a matrix creates a vector
  • empty subsetting vectors > all entries

By Name

rownames(mat) <- c("a", "b")
colnames(mat) <- c("i", "ii")
mat
  i ii
a 1  3
b 2  4
mat["a", "ii"]
[1] 3

By Logical

mat[c(TRUE, FALSE), c(FALSE, TRUE)]
[1] 3



You can mix and match

mat["a", c(FALSE, TRUE)]
[1] 3

What’s going on here?

mat
  i ii
a 1  3
b 2  4
mat_log
      [,1]  [,2]
[1,]  TRUE  TRUE
[2,] FALSE FALSE

Question

What will the following command produce? Why?

mat[mat_log]
00:45

Matrices in the Monastery

Load from lda package

# install.packages("lda") # install lda package
library(lda) # load lda package
data(sampson) # load data
samplik <- sampson[["SAMPLK1"]]
sampdlk <- sampson[["SAMPDLK"]]

Object 1: Likes

samplik

How do you extract

  1. The way Peter ranked the others?
  2. The way the others ranked Peter?

Summarizing Monks

peter <- samplik[, "PETER_4"]
mean(peter)
ambrose <- samplik[, "AMBROSE_9"]
mean(ambrose)

Total likes (to others)

rowSums(samplik)

Total likes (from others)

colSums(samplik)
sort(colSums(samplik))

Object 2: Dislikes

sampdlk
sort(colSums(sampdlk), decreasing = TRUE)

Affective Matrix

aff_mat <- samplik - sampdlk
aff_mat
sort(colSums(aff_mat), decreasing = TRUE)

Visualizing network data

Vectors

  • Central data structure in R
    • Vectorization
    • Vector Recycling
  • Ordered set of elements
    • Subsetting with []
  • Atomic: elements of the same type
    • Type coersion

Matrices

  • A vector with dimension info
    • Vectorization
    • Vector Recycling
  • Ordered set of elements
    • Subsetting with [vec_ind] or [row,col]
  • Also Atomic

Vectors

  • Central data structure in R
    • Vectorization
    • Vector Recycling
  • Ordered set of elements
    • Subsetting with []
  • Atomic: elements of the same type
    • Type coersion

Matrices

  • A vector with dimension info
    • Vectorization
    • Vector Recycling
  • Ordered set of elements
    • Subsetting with [vec_ind] or [row,col]
  • Also Atomic

Vectors

  • Central data structure in R
    • Vectorization
    • Vector Recycling
  • Ordered set of elements
    • Subsetting with []
  • Atomic: elements of the same type
    • Type coersion

Matrices

  • A vector with dimension info
    • Vectorization
    • Vector Recycling
  • Ordered set of elements
    • Subsetting with [vec_ind] or [row,col]
  • Also Atomic