#HOMEWORK 4

Nicolás Zapata

1

Suppose x = 1.1, a = 2.2, and b = 3.3. Assign each expression to the value of the variable z and print the value stored in z.

𝑥𝑎𝑏 (𝑥𝑎)𝑏 3𝑥3+2𝑥2+1

# First, assign the values to x, a and b
x <- 1.1
a <- 2.2
b <- 3.3 

# Create a vector with the formula
z <- x^(a^b)
print(z)
## [1] 3.61714
z <- (x^a)^b
print(z)
## [1] 1.997611
z <- 3*(x^3)+2*(x^2)+1
print(z)
## [1] 7.413

2

Using the rep and seq functions, create the following vectors: a. (1,2,3,4,5,6,7,8,7,6,5,4,3,2,1) b. (1,2,2,3,3,3,4,4,4,4,5,5,5,5,5) c. (5,4,4,3,3,3,2,2,2,2,1,1,1,1,1)

my_vector1 <- c(seq(from=1 , to=8), seq(from=7, to=1)) 
print(my_vector1)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
my_vector2 <- c(rep(1:5,1:5))
print(my_vector2)
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
my_vector3 <- c(rep(5:1,1:5))
print(my_vector3)
##  [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1

Use the function seq to create the sequence and rep to determine how many times the the numbers repeat

3

Create a vector of two random uniform numbers. In a spatial map, these can be interpreted as x and y coordinates that give the location of an individual (such as a marked forest tree in a plot that has been mapped). Using one of R’s inverse trigonometry functions (asin(), acos(), or atan()), convert these numbers into polar coordinates

vector3 <- runif(2)
print(vector3)
## [1] 0.04335591 0.01959930
asin(vector3)
## [1] 0.04336951 0.01960055
acos(vector3)
## [1] 1.527427 1.551196
atan(vector3)
## [1] 0.04332878 0.01959679

4

Create a vector queue <- c(“sheep”, “fox”, “owl”, “ant”) where queue represents the animals that are lined up to enter Noah’s Ark, with the sheep at the front of the line. Using R expressions, update queue as:

the serpent arrives and gets in line; the sheep enters the ark; the donkey arrives and talks his way to the front of the line; the serpent gets impatient and leaves; the owl gets bored and leaves; the aphid arrives and the ant invites him to cut in line. Finally, determine the position of the aphid in the line.

queue <- c("sheep", "fox", "owl", "ant")
print(queue)
## [1] "sheep" "fox"   "owl"   "ant"
queue <- c(queue, "serpent")
print(queue)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"
queue <- queue[queue !="sheep"]
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"
#other way
queue[c(1,2)]
## [1] "fox" "owl"
queue[-c(1)]
## [1] "owl"     "ant"     "serpent"
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"
queue <- c("donkey", queue)
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
queue <- queue[queue !="serpent"]
print(queue)
## [1] "donkey" "fox"    "owl"    "ant"
queue <- queue[queue !="owl"]
print(queue)
## [1] "donkey" "fox"    "ant"
queue <- c(queue[1:2], "aphid", queue[3])  
print(queue)
## [1] "donkey" "fox"    "aphid"  "ant"
position <- which(queue == "aphid")
print(position)
## [1] 3

5

z <- seq(1,100) 
print(z)
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
z <- 1:100
print(z)
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
filtered_numbers <- z[which(z %% 2 != 0 & z %% 3 != 0 & z %% 7 != 0)]
print(filtered_numbers)
##  [1]  1  5 11 13 17 19 23 25 29 31 37 41 43 47 53 55 59 61 65 67 71 73 79 83 85
## [26] 89 95 97

Generate a sequence from 1 to 100. Use “%%” to determine the remainder when each number is divided.