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.
x <- 1.1
a <- 2.2
b <- 3.3
# a.
z <- x^(a^b)
print(z)
## [1] 3.61714
#[1] 7.413
# b.
z <- (x^a)^b
print(z)
## [1] 1.997611
#[1] 3.61714
#c
z <- ((3*x^3)+(2*x^2)+1)
print(z)
## [1] 7.413
#[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)
a <- c(seq(1,8),seq(7,1))
print(a)
## [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
b <- rep(x=seq(1,5),times=seq(1,5))
print(b)
## [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
c <- rep(x=seq(5,1),times=seq(1,5))
print(c)
## [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).
need to find r and theta
x <- runif(100)[1]
y <- runif(100)[1]
ccoord <- c(x,y)
r <- sqrt((x^2)+(y^2))
theta <- asin(y/r)
pcord <- c(r,theta)
print(ccoord)
## [1] 0.3332541 0.1567834
print(pcord)
## [1] 0.3682924 0.4397393
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:
ss <- "serpent"
dk <- "donkey"
ap <- "aphid"
#0
queue <-c("sheep","fox","owl","ant")
print(queue)
## [1] "sheep" "fox" "owl" "ant"
#1
queue <- c(queue,ss)
print(queue)
## [1] "sheep" "fox" "owl" "ant" "serpent"
#2
queue <- queue[2:length(queue)]
print(queue)
## [1] "fox" "owl" "ant" "serpent"
#3
queue <- c(dk,queue)
print(queue)
## [1] "donkey" "fox" "owl" "ant" "serpent"
#4
queue <- queue[!queue=="serpent"]
print(queue)
## [1] "donkey" "fox" "owl" "ant"
#5
queue <- queue[!queue=="owl"]
print(queue)
## [1] "donkey" "fox" "ant"
#6
pant <- which(queue=="ant")
queue <- c(queue[1:pant-1],"aphid",queue[pant:length(queue)])
print(queue)
## [1] "donkey" "fox" "aphid" "ant"
#7
which(queue=="aphid")
## [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.
hun <- c(1:100)
hunr <- (hun%%2==0) | (hun%%3==0) | (hun%%7 == 0)
grab <- which(hunr==FALSE)
num <- hun[grab]
print(num)
## [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