Ch1.2.2 Data Structures

What is a Data Structure?

A data structure is:

  • a collection of data values together with
  • the functions or operations that can be applied to the data

Examples of Data Structures

Some useful data structures for numerical analysis include:

  • vectors
  • lists
  • matrices
  • data frames

Vectors

The vector object is a single dimensional ordered list of data.

  • A row vector looks like this:
(x <- c(1, 3, 5))
[1] 1 3 5
  • Since the order of entries matters, here's a different vector:
(y <- c(5, 3, 1))
[1] 5 3 1

Vectors

A column vector looks like this:

x <- c(1, 3, 5)
t(t(x))
     [,1]
[1,]    1
[2,]    3
[3,]    5

Vectors: Try These

Open R Studio and enter in the following vectors. Be prepared to share your screen.

  • Vector 1:
(x <- c(1, 1, 2, 3, 5))
[1] 1 1 2 3 5
  • Vector 2:
(y <- c(5, 8, 13, 21))
[1]  5  8 13 21

Vector Entries

  • To obtain the 2nd entry in a vector x, type x[2]
x <- c(1, 1, 2, 3, 5)
x[2]
[1] 1
  • 4th entry: (You try)

Voice Wave Vector

In Math 362 Fourier Analysis, we use Audacity to record sound waves.

oo wave

Voice Wave Vector

A sound wave vector is a list of ordered values.

  • Here are the first ten values of me saying “ooooo”:
(x <- c(0.109, 0.125, 0.141, 0.148, 0.164, 0.172, 0.180, 0.188, 0.195, 0.195))
 [1] 0.109 0.125 0.141 0.148 0.164 0.172 0.180 0.188 0.195 0.195

Voice Wave Vector

x <- c(0.109, 0.125, 0.141, 0.148, 0.164, 0.172, 0.180, 0.188, 0.195, 0.195)

Here's a graph of this vector:

plot of chunk unnamed-chunk-9

Signal Processing

A simple application of processing a sound wave is thresholding.

  • Here's the original vector x:
x <- c(0.109, 0.125, 0.141, 0.148, 0.164, 0.172, 0.180, 0.188, 0.195, 0.195)
  • Here's the thresholded vector y using cutoff value r = 0.15:
y <- c(0, 0, 0, 0, 0.164, 0.172, 0.180, 0.188, 0.195, 0.195)

Signal Processing

Here's a graph of the original vector:

plot(x)

plot of chunk unnamed-chunk-12

Signal Processing

Here's a graph of the thresholded vector:

plot(y)

plot of chunk unnamed-chunk-13

Raw Thresholding Voice Wave

Full oo wave, thresholded at the 40% level:

oo wave

Vectors with Non-numerical Values

As ordered lists, vectors can hold non-numerical values:

-Using single quotes:

(x <- c('cat', 'dog', 'horse', 'bird'))
[1] "cat"   "dog"   "horse" "bird" 

-Using double quotes:

  (x <- c("cat", "dog", "horse", "bird"))
[1] "cat"   "dog"   "horse" "bird" 

Vectors with Non-numerical Values

Check for equality of entries:

x <- c("cat", "dog", "horse", "bird")
y <- c("bird", "dog", "horse", "cat")
x == y
[1] FALSE  TRUE  TRUE FALSE

Lists

List are nonordered data structures.

(x <- list('cat', 'dog', 'horse', 'bird'))
[[1]]
[1] "cat"

[[2]]
[1] "dog"

[[3]]
[1] "horse"

[[4]]
[1] "bird"

Lists

Using double quotes:

(x <- list("cat", "dog", "horse", "bird"))
[[1]]
[1] "cat"

[[2]]
[1] "dog"

[[3]]
[1] "horse"

[[4]]
[1] "bird"

Lists

Entry labels can be used.

(x <- list(a = "cat", b = "dog", c = "horse", d = "bird"))
$a
[1] "cat"

$b
[1] "dog"

$c
[1] "horse"

$d
[1] "bird"

Matrices

Example

(m = matrix(c(3,-4.2,-7.1,0.95),nrow=2,ncol=2))
     [,1]  [,2]
[1,]  3.0 -7.10
[2,] -4.2  0.95
m[2,1]
[1] -4.2

Matrices

Example

(m = matrix(1:6, nrow=2, byrow=T))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
m[2,3]
[1] 6

Matrices (Row call)

(m = matrix(1:6, nrow=2, byrow=T))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
m[2,]
[1] 4 5 6

Matrices (Column call)

(m = matrix(1:6, nrow=2, byrow=T))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
m[ ,3]
[1] 3 6

Matrices (Transpose)

(m = matrix(1:6, nrow=2, byrow=T))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
t(m)
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6