Objectives

The objectives for this assignment are to learn some R fundamentals, including:

  • Variables and data types:
  • numeric
  • boolean
  • factor
  • basic data structures:
  • vectors
  • matrices
  • data frames
  • lists
  • bubsetting data structures:
  • by name
  • by position

Instructions

Hint: you should read through the template R-script before you complete the DataCamp course. It will be helpful to work on the questions in the script as you complete the corresponding sections in the DataCamp assignment.

  1. Follow the DataCamp invite link and complete the Introduction to R course that is assigned to you.

  2. Create an R script to work through the questions at the end of this page.

  • Create an R script with the filename data_camp_1_draft.R
  • You will work on the assignment code in this document.
  • Remember to save your file often, RStudio does not auto-save.
  1. Complete the R code needed to answer the questions in the draft script.
  • You should write down lots of comments to remind yourself of what your are doing, what worked, and what didn’t.
  • It’s OK if your draft script is messy, it is a work in progress.
  1. When you are satisfied with your answers, make a copy of your draft script with the filename data_camp_1_final.R and submit it via the file upload field on the assignment page in Moodle.

Code comments

Your final R-script will be graded on whether the code runs, is correct, and your comments.

Comments are lines within an R-script that begin with the number sign, or hash symbol: #

  • This is known as the comment character.

When r executes code in your script, it ignores all of the text on a line following the comment character.

Here is an example of how it works:

# I am going to add two numbers together, and then multiply the sum:

(4 + 8) * 2
## [1] 24
# This is what I tried to do first, but it didn't work:
# 4 + 8) * 2

Notice that R ignored the comment lines, and performed the calculation on the uncommented line.

Comments are an important form of documentation.

Good comments allow you, or someone else, to understand what the code itself is doing. The explain both what the objective of the code is, and important technical details of how the code itself works.

You’ll find that uncommented scripts can be difficult or impossible to read or understand. Uncommented code is a lot like data without metadata!

Deliverables

R-Script: total of 30 pts

You must upload an R script that includes answers to the following questions

the R script you submit must be neatly formatted and your code answer to every question must be accompanied by corresponding code comments that briefly explain what your code accomplishes.

  • See the section on code comments above for more info.

Variables: Q1 - Q2

  • Q1 (2 pts.): Create a logical variable called ‘aaa’ that holds a value of TRUE.

  • Q2 (2 pts.): Create a two numeric variables called ‘one’ and ‘four’.

    • They should contain the values 1 and 4, respectively

Vectors: Q3 - Q6

  • Q3 (2 pts.): Create a vector called vec_1 that contains a sequence of integers from 1 to 10.

  • Q4 (2 pts.): Create a vector called vec_2 that contains values that are three times the values of the elements of vec_1.

  • Q5 (2 pts.): Create a vector called vec_3 whose elements are the product of the corresponding elements in vec_1 and vec_2.

  • Q6 (2 pts.): Create a vector called vec_4 whose elements are the integers from 1 to 12.

Matrices: Q7 - Q8

  • Q7 (2 pts.): Create a matrix mat_1 from vec_4 that has three rows and four columns.
    • The values in mat_1 should be increasing by row. For example, the first row of mat_1 should contain the values 1, 2, 3, 4.
  • Q8 (2 pts.): Create a matrix mat_2 (again using vec_4) with four rows and three columns.
    • The values in mat_2 should be increasing by column. For example, the first row of mat_2 should contain the values 1, 5, 9.

Data Frames: Q9 - Q11

Re-create the data frame planets_df from the DataCamp course.

  • You can re-visit the CataCamp course to see how you built it there.
  • Q9 (2 pts.): Extract the ‘rotation’ column from planets_df and save it to a vector called rot

  • Q10 (2 pts.): Extract the value of planets_df in row 2, column 3 and save it to a variable called var_1

  • Q11 (2 pts.): Extract the even-numbered rows of planets_df and save them to a new data frame called planets_2

R Script Formatting

  • Q12 (8 pts.): R Script runs successfully in a fresh R session, is neatly formatted, and includes appropriate comments.