A data structure is:
Some useful data structures for numerical analysis include:
The vector object is a single dimensional ordered list of data.
(x <- c(1, 3, 5))
[1] 1 3 5
(y <- c(5, 3, 1))
[1] 5 3 1
A column vector looks like this:
x <- c(1, 3, 5)
t(t(x))
[,1]
[1,] 1
[2,] 3
[3,] 5
Open R Studio and enter in the following vectors. Be prepared to share your screen.
(x <- c(1, 1, 2, 3, 5))
[1] 1 1 2 3 5
(y <- c(5, 8, 13, 21))
[1] 5 8 13 21
x <- c(1, 1, 2, 3, 5)
x[2]
[1] 1
In Math 362 Fourier Analysis, we use Audacity to record sound waves.
A sound wave vector is a list of ordered values.
(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
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:
A simple application of processing a sound wave is thresholding.
x <- c(0.109, 0.125, 0.141, 0.148, 0.164, 0.172, 0.180, 0.188, 0.195, 0.195)
y <- c(0, 0, 0, 0, 0.164, 0.172, 0.180, 0.188, 0.195, 0.195)
Here's a graph of the original vector:
plot(x)
Here's a graph of the thresholded vector:
plot(y)
Full oo wave, thresholded at the 40% level:
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"
Check for equality of entries:
x <- c("cat", "dog", "horse", "bird")
y <- c("bird", "dog", "horse", "cat")
x == y
[1] FALSE TRUE TRUE FALSE
List are nonordered data structures.
(x <- list('cat', 'dog', 'horse', 'bird'))
[[1]]
[1] "cat"
[[2]]
[1] "dog"
[[3]]
[1] "horse"
[[4]]
[1] "bird"
Using double quotes:
(x <- list("cat", "dog", "horse", "bird"))
[[1]]
[1] "cat"
[[2]]
[1] "dog"
[[3]]
[1] "horse"
[[4]]
[1] "bird"
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"
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
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
(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
(m = matrix(1:6, nrow=2, byrow=T))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
m[ ,3]
[1] 3 6
(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