Learning objectives

  • Gain familiarity the RStudio IDE
  • Manage your workspace in an interactive R session
  • Understand variable assignment
  • Use load() to import a data set
  • Take a look at a data frame
  • Add columns, subset, and summarize a data frame
  • To be able to seek help via ? and Google

Challenge 1 – Make a new directory and project for DIBSI Day 2

  • In your project directory, either using the Project tab of RStudio or your OS’ file system, create the following directories:
    • data
    • scripts
    • results
  • Create a new .R script file in the scripts/ folder calling it whatever you like

MCQ – Variable Assignment

What does the following code print?

a <- 1
b <- 2
c <- a + b
b <- 4
a <- b
c <- a
c 
Option 1) a  
Option 2) 3  
Option 3) 4  
Option 4) ::nothing::  

Challenge 2 – Extra practice: Assignment & Comparison

Which elephant weighs more? Convert one’s weight to the units of the other, and store the result in an appropriately-named new variable. Write a command to test whether elephant1 weights more than elephant2 (1 kg ≈ 2.2 lb).

elephant1_kg <- 3492
elephant2_lb <- 7757

MCQ – Subset and vectorize

Load the continents data frame again. Make a copy from your day 1 project folder, and move it to the data folder you just created for day 2.

What is the total land area of continents that have at least 10% of the world’s population?

  • Use subsetting to get the areas you want
  • Use the sum() function to find the total land area
Option 1) 149428500
Option 2) 126700000
Option 3) 22728500
Option 4) 100

Data types in R

For a brief note on data types in R, we turn to the Data Carpentry: R for data analysis and visualization of Ecological Data Introduction to R materials.


This lesson is adapted from the Software Carpentry: R for Reproducible Scientific Analysis Vectors and Data Frames materials and the Data Carpentry: R for data analysis and visualization of Ecological Data Introduction to R materials.