Exercise 1A: Introduction to R

Author

HeaDS Data Science Lab, University of Copenhagen

File tree

  1. Make a new directory for this course.

  2. Go to course website and to the Data tab. Press the Download Data button.

  3. Move the Data folder to your course directory.

  4. Under your course directory, make a new folder for your scripts and another for the presentations you download.

Your file tree should look something like this:

Getting started with Rstudio

  1. Start RStudio.

  2. In the tool bar, click on the file icon with the β€˜+’ sign on the very left β†’ R script. This is a β€˜flat’ R script that we showed in the beginning. We will not be working in this format in the course, but it is important that you know what they are for you further R adventures.

  3. Save the file in your scripts directory.

  4. In the bottom-right panel, navigate to the file you just saved.

When doing data science, you often work with some kind of input files, like an Excel sheet from the lab. It is therefore important that you know how to navigate to files from your R script. The directories you need to go through to find your input file can either be absolute or relative:

  • Absolute path: The path from the root of your file system to the input file.

  • Relative path: The path from the working directory to the input file.

We will try to find both paths to the climate.xlsx file in the Data directory.

Step 1: Absolute Path

  1. Create a character variable called absolute_path like the one below.
absolute_path <- "~/"
  1. Put your cursor between the slash and the last quotation mark and press the tab key. Navigate to the directory containing your input file.
absolute_path <- "~/Desktop/Stuff/Courses/GreatestCourse/FromExceltoR/Data/climate.xlsx"

Step 2: Relative Path

  1. Change your working directory to an arbitrary place using setwd()
  2. In your script, create another character variable called relative_path in your R script.
  3. Follow these steps:
  • Put your cursor between the two quotation marks.

  • Press the tab key. What do you see?

  • Write two periods and a slash (../):

  • Now place the cursor between the slash and the quotation mark and press the tab key again. What do you see?

  • Use the tab key to navigate to the climate.xlsx file through the simplest path. NB This might include deleting the initial ../.

setwd("~/Desktop/Stuff/Courses/GreatestCourse/FromExceltoR")
relative_path <- "/Data/climate.xlsx"
setwd("~/Desktop/Stuff/Courses/GreatestCourse/FromExceltoR/Scripts")
relative_path <- "../Data/climate.xlsx"
setwd("~/Desktop/Stuff/Projects/RNAseq/TheGreatProject")
relative_path <- "../../../Courses/GreatestCourse/FromExceltoR/Data/climate.xlsx"