Atomic Vectors and Basic R Lab

Question #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.

  1. \(x^{a^b}\)
x <- 1.1

a <- 2.2

b <- 3.3

z <- x^{a^b}
print(z)
## [1] 3.61714
  1. (xa)b
x <- 1.1

a <- 2.2

b <- 3.3

z <- (x^a)^b
print(z)
## [1] 1.997611
  1. 3x3+2x2+1
x <- 1.1

a <- 2.2

b <- 3.3

z <- (3*(x^3))+2*(x^2)+1
print(z)
## [1] 7.413

Question #2

Using the rep and seq functions, create the following vectors:

  1. (1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)
a <- seq(from = 1, to = 7, by = 1)

b <- seq(from = 8, to = 1, by = -1)

z <- c(a,b)
print(z)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
  1. (1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)
my_vec<- c(1,2,3,4,5)
rep(x=my_vec, time=my_vec)
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
  1. (5,4,4,3,3,3,2,2,2,2,1,1,1,1,1)
my_vec<-c(5,4,3,2,1)
rep(x=my_vec, time= c(1,2,3,4,5))
##  [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1

Question #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

set.seed(1000)
xy_coordinates <- runif(2)
polar_coordinates_r <- sqrt(xy_coordinates[1]+xy_coordinates[2])
print(polar_coordinates_r)
## [1] 1.042461
polar_coordinates_0 <- atan(xy_coordinates[2]/xy_coordinates[1])
print(polar_coordinates_0)
## [1] 1.162948
polar_coordinates <- c(polar_coordinates_r, polar_coordinates_0)
print(polar_coordinates)
## [1] 1.042461 1.162948

Question #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:

queue <- c("sheep", "fox", "owl", "ant")

# a. the serpent arrives and gets in line;
queue <- c(queue, "serpent")
print (queue)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"
# b. the sheep enters the ark;
queue <- queue[-1]
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"
# c. the donkey arrives and talks his way to the front of the line;
queue <- c("donkey", queue)
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
# d. the serpent gets impatient and leaves;
queue <- queue[-5]
print(queue)
## [1] "donkey" "fox"    "owl"    "ant"
# e. the owl gets bored and leaves;
queue <- queue[-3]
print(queue)
## [1] "donkey" "fox"    "ant"
# f. the aphid arrives and the ant invites him to cut in line.
queue <- append(queue, "aphid", after=2)
print(queue)
## [1] "donkey" "fox"    "aphid"  "ant"
# g. Finally, determine the position of the aphid in the line.
which(queue=="aphid")
## [1] 3

Question #5

Use R to create a vector of all of the integers from 1 to 100 that are not divisible by 2, 3, or 7.

my_vec<- seq(1,100) # making vector sequence 1 to 100

print(my_vec)
##   [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
#my_vec <- my_vec[which((my_vec%%2) >0)] # %% gives remainders after dividing, >=1 selecting those with a remainder greater than or equal to 1
#print(my_vec)
 
my_vec2<- my_vec[which((my_vec%%2)>=1 & (my_vec%%3)>=1 & (my_vec%%7)>=1)]

print(my_vec2)
##  [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