Feel free to mark using the rpubs document. https://rpubs.com/PontSatyre11119/basic-r_eeb313
To submit this assignment, upload the full document to Quercus,
including the original questions, your code, and the output. Submit your
assignment as a knitted .pdf (preferred),
.docx, or .html file.
Vectors (2 marks)
v with all integers 0-20, and a vector
w with every third integer in the same range. (0.25
marks)v <- seq(0,20)
w <- seq(0,20,3)
v
## [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
w
## [1] 0 3 6 9 12 15 18
b. How much longer is vector `v` compared with `w`?
(0.25 marks)
length(v) - length(w)
## [1] 14
c. Create a new vector, `v_square`, with the square of elements at indices
3, 6, 7, 10, and 15 from the variable `v`. _Hint:
Use indexing rather than a for loop._ (0.5 marks)
v_square <- (v[c(4, 7, 8, 11, 16)])^2
v_square
## [1] 9 36 49 100 225
d. Calculate the mean and median of the first four values from
`v_square`. (0.5 marks)
mean(v_square[seq(1,4)])
## [1] 48.5
median(v_square[seq(1,4)])
## [1] 42.5
e. Create a logical (or boolean) vector `v_bool` to indicate which vector `v_square`
elements are bigger than 30. How many values are over 30? _Hint:
In R, TRUE = 1, and FALSE = 0, so you can use simple arithmetic
to find this out._ (0.5 marks)
v_bool <- length(v_square > 30)
v_bool
## [1] 5
Write a function that calculates the mean of the last two elements of
any numeric vector. Test this function with the v and
v_square vectors.
last_2_ele_func <- function(num_vec){
((num_vec[length(num_vec)])+(num_vec[length(num_vec)-1]))/2
} # adds last and second last values, divides by 2
last_2_ele_func(v)
## [1] 19.5
last_2_ele_func(v_square)
## [1] 162.5
Turn the given data frame into an exponent table, a table that shows you results of raising one number (the base, rows) to the power of another (the exponent, columns). See an example here. Use this data frame to find 5 to the power 9. Hint: use a nested for-loop to fill your data frame
exp_table <- data.frame(matrix(ncol=10, nrow=10))
n <- 10 # how many values we want (n-val)
exp_table <- data.frame() # making new df
for(i in 0:n){ # for every 0 to n-val,
df <- (0+i)^(0:n) # calculate the exp from 0 to n-val, of each 0 to the i-th value
exp_table <- rbind(exp_table, df) # makes a table
}
colnames(exp_table) <- c(0:n) # renames columns
rownames(exp_table) <- c(0:n) # renames rows
exp_table
## 0 1 2 3 4 5 6 7 8 9 10
## 0 1 0 0 0 0 0 0 0 0 0 0
## 1 1 1 1 1 1 1 1 1 1 1 1
## 2 1 2 4 8 16 32 64 128 256 512 1024
## 3 1 3 9 27 81 243 729 2187 6561 19683 59049
## 4 1 4 16 64 256 1024 4096 16384 65536 262144 1048576
## 5 1 5 25 125 625 3125 15625 78125 390625 1953125 9765625
## 6 1 6 36 216 1296 7776 46656 279936 1679616 10077696 60466176
## 7 1 7 49 343 2401 16807 117649 823543 5764801 40353607 282475249
## 8 1 8 64 512 4096 32768 262144 2097152 16777216 134217728 1073741824
## 9 1 9 81 729 6561 59049 531441 4782969 43046721 387420489 3486784401
## 10 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000
exp_calculator <- function(b, e){
exp_table[b+1,e+1]
}
exp_calculator(b = 5, e = 9) # where b is base and e is exponent
## [1] 1953125
Data frames (2 marks)
beaver1? How many observations (rows)
and variables (columns) are there? (0.5 marks)colnames(beaver1)
## [1] "day" "time" "temp" "activ"
dim(beaver1)
## [1] 114 4
b. Display both the first 6 and last 6 rows of this data frame.
Show how to do so with both indexing as well as specialized functions. (0.5 marks)
head(beaver1)
## day time temp activ
## 1 346 840 36.33 0
## 2 346 850 36.34 0
## 3 346 900 36.35 0
## 4 346 910 36.42 0
## 5 346 920 36.55 0
## 6 346 930 36.69 0
tail(beaver1)
## day time temp activ
## 109 347 250 36.84 0
## 110 347 300 36.86 0
## 111 347 310 36.88 0
## 112 347 320 36.93 0
## 113 347 330 36.97 0
## 114 347 340 37.15 1
beaver1[1:6,]
## day time temp activ
## 1 346 840 36.33 0
## 2 346 850 36.34 0
## 3 346 900 36.35 0
## 4 346 910 36.42 0
## 5 346 920 36.55 0
## 6 346 930 36.69 0
beaver1[(nrow(beaver1)-6):(nrow(beaver1)),]
## day time temp activ
## 108 347 240 36.82 0
## 109 347 250 36.84 0
## 110 347 300 36.86 0
## 111 347 310 36.88 0
## 112 347 320 36.93 0
## 113 347 330 36.97 0
## 114 347 340 37.15 1
c. What is the difference in mean body temperature of the beaver when it was inside versus outside of the retreat? (0.5 marks)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# assuming variables 0 and 1 for beaver1$activ, where 0 is inside and 1 is outside
(beaver1 %>%
filter(activ == c(0,1)) %>% # concatenate 0 and 1
group_by(activ) %>% # group by 0 and 1
summarize(mean = mean(temp)) %>% # mean of groups 0 and 1
mutate(difference = diff(mean)))[2,3] # subset the new column with difference
## # A tibble: 1 x 1
## difference
## <dbl>
## 1 0.375
## # A tibble: 2 x 3
## activ mean difference
## <dbl> <dbl> <dbl>
## 1 0 36.8 0.375
## 2 1 37.2 0.375
d. How much did the body temperature of the beaver fluctuate (i.e., range) from 9:00-10:00 AM on the first day of the study? (0.5 marks)
beaver1 %>%
filter(day == 346 & between(time, 900, 1000)) %>% # only first day & between 9am to 10am
select(temp) %>% # only temp
range() %>% # max and min in temp
diff() %>% # difference between max and min
abs() # makes fluctuation result always positive
## [1] 0.46