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.
xab
(xa)b
3x3+2x2+1
## [1] 3.61714
## [1] 1.997611
## [1] 7.413
Using the rep and seq functions, create the following vectors:
(1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)
(1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)
(5,4,4,3,3,3,2,2,2,2,1,1,1,1,1)
## Part 1
ascend <- seq(1,8) # Creates seq of integers from 1 to 8, since no step is specified, 1 is used by default
descend <- seq(8,1) # Creates seq of integers from 8 to 1
a1 <- c(ascend, descend[-1]) # Concatanates both lists into one list while removing the first element/value of list descend, in this case it is to avoid repetition of 8
print(a1)
## [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
## Part 2
my_vec <- c(1,2,3,4,5) # Concatanate 1, 2, 3, 4, 5 to a new vector called my_vec
a2 <- rep(x = my_vec, times = my_vec) # Creates new list of elements from my_vec, and each element is repeated the number of times corresponding to my_vec as well
# In this case, 1 is repeated once, 2 repeated twice, 3 repeated thrice and so on
print(a2)
## [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
## Part 3
my_vec2 <- c(5, 4, 3, 2 ,1) # Concatanate 5, 4, 3, 2, 1, to a new vector called my_vec2
a3 <- rep(x = my_vec2, times = my_vec) # Similar to above part but now 5 is repeated once, 4 twice, 3 thrice, and so on as our list is now a count down from 5 to 1
print(a3)
## [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1
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 (If you don’t know what polar coordinates are, read about them on the web here, here, or in your calculus textbook).
set.seed(1201) # Setting seed for rand numb generation for repeatability
cartesian <- c(runif(1),runif(1)) # creating list of cartesian coordinates
print(cartesian)
## [1] 0.03732745 0.84894747
r <- sqrt((cartesian[1]^2) + (cartesian[2]^2)) # finding r for polar using pythagorean theorem
theta <- atan(cartesian[2]/cartesian[1]) # cart[2] = y, cart[1] = x, corresponding to (x,y) in normal cartesian coordinates, finding theta for polar
polar <- c(r, theta)
print(polar)
## [1] 0.8497677 1.5268555
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.
# Part 1 the serpent arrives and gets in line;
queue <- c("sheep", "fox", "owl", "ant") # Creates list of queue
print(queue)
## [1] "sheep" "fox" "owl" "ant"
queue <- append(queue, "snake") # Adds value/element "snake" to the end of list, if after is not declared will add to end of list
print(queue)
## [1] "sheep" "fox" "owl" "ant" "snake"
# Part 2 the sheep enters the ark;
queue <- queue[-1] # Removes head/first element of the list
print(queue)
## [1] "fox" "owl" "ant" "snake"
# Part 3 the donkey arrives and talks his way to the front of the line;
queue <- append(queue, "donkey", after = 0) # adds "donkey"/element to front of the list with after = 0
print(queue)
## [1] "donkey" "fox" "owl" "ant" "snake"
# Part 4 the snake/serpent gets impatient and leaves;
queue <- queue[-5] # Removes fifth element of list, in this case, snake
print(queue)
## [1] "donkey" "fox" "owl" "ant"
# Part 5 the owl gets bored and leaves;
queue <- queue[-3] # Removes third element of list, in this case, Owl
print(queue)
## [1] "donkey" "fox" "ant"
# Part 6 the aphid arrives and the ant invites him to cut in line.
queue <- append(queue, "aphid", after = 2) # Add "aphid to list after element/position 2
print(queue)
## [1] "donkey" "fox" "aphid" "ant"
# Part 7 Finally, determine the position of the aphid in the line.
aphid_line_pos <- which(queue == "aphid") # Looks for element in list that matches "aphid" and returns the pos of the matching element
print(aphid_line_pos)
## [1] 3
Use R to create a vector of all of the integers from 1 to 100 that are not divisible by 2, 3, or 7. You will need one of the arithmetic operators on this cheat sheet.
weird_vec <- seq(1:100) # Creates vector of integers from 1 to 100
typeof(weird_vec) # confirms list of Integers
## [1] "integer"
pos_weird_vec <- (weird_vec %% 2) == 0 | (weird_vec %% 7) == 0 | (weird_vec %% 3) == 0
# The above method looks for values in the last the give 0 as a remainder when divided by 2, 3, or 7 - ensuring we only have numbers divisible by any of these numbers
pos_weird_vec
## [1] FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE
## [13] FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE
## [25] FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
## [37] FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE
## [49] TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE
## [61] FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE
## [73] FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE
## [85] FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE
## [97] FALSE TRUE TRUE TRUE
## [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