AN588-Homework1-itsjon

My Attempt into the first homework

Challenge 1

library(stringr) ulysses_lines <- “Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of lather on which a mirror and a razor lay crossed.”

Ulysses_remove <- gsub(“[[:punct:]]”, ““, ulysses_lines)

ulysses_remove <- gsub(“[[:punct:]]”, ““, ulysses_lines)

ulysses_words <- str_split(ulysses_remove, ” “)[[1]]

every_third_word <- ulysses_words[seq(1, length(ulysses_words), by = 3)]

every_third_word [1] “Stately” “Mulligan” “the” [4] “a” “lather” “a”
[7] “a” “crossed”

Challenge 2

Suppose m <- matrix(data = 1:80, nrow = 8, ncol = 10, byrow = FALSE)

Extract and assign columns to x

> x <- m[, c(2,3,6)]

> x [,1] [,2] [,3] [1,] 9 17 41 [2,] 10 18 42 [3,] 11 19 43 [4,] 12 20 44 [5,] 13 21 45 [6,] 14 22 46 [7,] 15 23 47 [8,] 16 24 48 2, 3, 6:

> x <- m[, c(2,3,6)]

> x [,1] [,2] [,3] [1,] 9 17 41 [2,] 10 18 42 [3,] 11 19 43 [4,] 12 20 44 [5,] 13 21 45 [6,] 14 22 46 [7,] 15 23 47 [8,] 16 24 48

Rows 6-8:

> x <- m[6:8,] > x [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 14 22 30 38 46 54 62 70 78 [2,] 7 15 23 31 39 47 55 63 71 79 [3,] 8 16 24 32 40 48 56 64 72 80

Row 2, Column 2 -> Row 6, Column 9:

> x <- m[2:6, 2:9] >

x

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 10 18 26 34 42 50 58 66

[2,] 11 19 27 35 43 51 59 67

[3,] 12 20 28 36 44 52 60 68

[4,] 13 21 29 37 45 53 61 69

[5,] 14 22 30 38 46 54 62 70

Challenge 3

My array: > a <- array(400:1, dim = c(5, 5, 4, 4))

Returns: a[1, 1, 1, 2] [300] a[2, 3, 2, ] [1] 364 264 164 64 a[1:5, 1:5, 3, 3] [,1] [,2] [,3] [,4] [,5] [1,] 150 145 140 135 130 [2,] 149 144 139 134 129 [3,] 148 143 138 133 128 [4,] 147 142 137 132 127 [5,] 146 141 136 131 126

Challenge 4

> Haplorhini <- list(Anthropoidea, Tarsioidea) >Catarrhini <- c(“Cercopithecidae”, “Hylobatidae”, “Hominidae”)

>Platyrrhini <- c(“Cebidae”, “Atelidae”, “Pitheciidae”)

> Anthropoidea <- list(Platyrrhini, Catarrhini)

> Tarsioidea = c(“Tarsiidae”)

> names(Haplorhini) <- list(“Anthropoidea”, “Catarrhini”)

Strepsirhini

Lemuroidea <- c(“Cheirogaleidae”, “Lepilemuridae”, “Indriidae”, “Lemuridae”, “Daubentoniidae”)

Lorisoidea <- c(“Lorisidae”, “Galagidae”)

Strepsirhini <- list(Lemuroidea, Lorisoidea)

names(Strepsirhini) <- list(“Lemuroidea”, “Lorisoidea”)

Primates names(Primates) <- list(“Haplorhini”, “Strepsirhini”)

Primates <- list(Haplorhini, Strepsirhini)

Primates

Challenge 5

numbers <- c(3, 0, 1, 23, 1, 2, 33, 1, 1, 42, 0, 1, 41, 0, 2)

m <- matrix(numbers, nrow = 5, ncol = 3, byrow = TRUE)

m

[,1] [,2] [,3]

[1,] 3 0 1

[2,] 23 1 2

[3,] 33 1 1

[4,] 42 0 1

[5,] 41 0 2

Data Frame

numbers <- c(3, 0, 1, 23, 1, 2, 33, 1, 1, 42, 0, 1, 41, 0, 2)

df <- as.data.frame(m)

df[, 2] <- as.logical(df[, 2])

df[, 3] <- as.factor(df[, 3])

df

V1 V2 V3

1 3 FALSE 1

2 23 TRUE 2

3 33 TRUE 1

4 42 FALSE 1

5 41 FALSE 2

str(df)

‘data.frame’: 5 obs. of 3 variables:

$ V1: num 3 23 33 42 41

$ V2: logi FALSE TRUE TRUE FALSE FALSE

$ V3: Factor w/ 2 levels “1”,“2”: 1 2 1 1 2 … Edits