The goal of this assignment is to continue our exploration of vectors, get you started with working in R, and introduce you to some common operations. Note: you may need to search the internet to find answers for some of the questions.
You will be graded as follows:
(20 pts) Have you completed the assignment in its entirety?
(20 pts) Are your responses correct for the subset of randomly graded questions?
(10 pts) CODE DOCUMENTATION. Add comments to nearly every line explaining what the line of code is doing.
Feel free to use the below tabs to check out some common functions.
Operator | Description |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
^ or ** |
Exponentiation |
%% |
Modulus/remainder (5 %% 2 is 1) |
%/% |
Integer division (5 %/% 2 is 2) |
Operator | Description |
---|---|
<, <= | Less than (or equal to) |
>, >= | Greater than (or equal to) |
== |
Equal to |
!= |
NOT equal to |
!x |
NOT x |
x | y |
x OR y |
x & y |
x AND y |
Function | Description |
---|---|
max | Maximum |
min | Minimum |
range | Range (min and max) |
prod | Product |
sum | Sum |
any | Any TRUE? |
all | All TRUE |
Function | Description |
---|---|
abs | Absolute value |
sign | Sign (positive or negative) |
sqrt | Square root |
ceiling | Round UP |
floor | Round DOWN |
round | Round |
trunc | Same as floor |
cumsum | Cumulative sum |
cumprod | Cumulative product |
cummin | Cumulative minimum |
cummax | Cumulative maximum |
log | Logarithm |
exp | Exponential/anti-log (e^?) |
pi | The constant, ‘PI’ |
This question was extracted from Lab 1B. Using the code you saved from the lab, proceed to part B.
Lab 1B asked the following:
Using the penguins
data from the palmerpenguin
package, create new vectors for the following variables: bill_length_mm
, bill_depth_mm
, flipper_length_mm
, body_mass_g
, island
, sex
, and species
. For each vector, find the appropriate statistics for the appropriate variable type, i.e. sum, mean, and standard deviation for numeric vectors and frequencies for character variables.
Create the following vectors:
bill_length_mm
and bill_depth_mm
.flipper_length_mm
and body_mass_g
.bill_length_mm
and body_mass_g
.bill_length_mm
, bill_depth_mm
, flipper_length_mm
and body_mass_g
.For the vectors found in question 1, find the following descriptive statistics: max, min, sum, mean, median, and standard deviation.
Create a 2 by 2 table showing the cross-tabulation of island and species.
Create a 2 by 2 table showing the table proportions of island and species.
Please read the “Debugging techniques” of Hadley Wickham’s “Advanced R” (freely available online). Describe the 4 steps to debugging your code. Record your results as comments on your R Script.
Repair the code below to print out the R object my_vector
. Record your results as comments on your R Script.
<- c(4,4,) vec_1
## Error in c(4, 4, ): argument 3 is empty
< c(5,6) vec_2
## Error in eval(expr, envir, enclos): object 'vec_2' not found
<- vec_1 * vec_2 My_vector
## Error in eval(expr, envir, enclos): object 'vec_1' not found
print(my_vector)
## Error in print(my_vector): object 'my_vector' not found
Describe two methods to open the help documentation in R? Record your results as comments on your R Script.
Other than the library
function, what are two other methods to call a function from an R Package?
Record your results as comments on your R Script.
Repair the following code to print out the value 10:
<- 10
my_variable my_varıable
## Error in eval(expr, envir, enclos): object 'my_varıable' not found
Describe what the functions sum
and cumsum
accomplishes:
<- c(1:10)
x sum(x)
cumsum(x)
Describe what the functions prod
and cumprod
accomplishes:
<- 1:10
y prod(y)
cumprod(y)
Describe what the %%
operator accomplish.
Record your results as comments on your R Script.
<- 11:15
z %% 2 z
Describe what the ==
operator accomplish.
Record your results as comments on your R Script.
<- 11:15
z %% 2 ) == 0 (z