Introduction to R

Files Handeling

Dr. Yu Cheng Hsu

Learning objectives

  • Demonstrate reading and loading data from the computer
  • Understand relative path and absolute path in the computer

Reading and writing data to file

For reading and writing data we recoomend use the library under tidyverse

#install tidyverse (package with lots of functions to handle data in R)
if (!requireNamespace("tidyverse", quietly = TRUE)) {    # checks if already installed
  install.packages("tidyverse")
}
library(tidyverse)                                       # load the package

Loading/saving data from folders

  • By default, R sets the working directory to your Documents folder.
  • Use setwd(PATH/TO/NEW/WORKING/DIRECTORY) to change the working directory.
  • Use getwd() to see your current working directory.

Consider the following file structure

/ (root)
├── home
│   └── user
│       ├── documents
│       │   ├── course
│       │   │   ├── THIS_RMD.rmd
│       │   │   └── myData.csv
│       │   └── notes.txt
│       └── downloads
|           └── IMAGE.png
└── var

Read the data

Using read_csv() to read the data

myData <- read_csv('myData.csv')
  • read_csv() need to specify WHERE do you put the file by Absolute path or Relative path
  • If you are using Rmarkdown (.rmd) to run the above code then you need to put in the same directory
  • Otherwise you need to put at your current working directory

File path

/ (root)
├── home
│   └── user
│       ├── documents
│       │   ├── course
│       │   │   ├── THIS_RMD.rmd
│       │   │   └── myData.csv
│       │   └── notes.txt
│       └── downloads
|           └── IMAGE.png
└── var

There are two ways of specifying a path:

  1. Absolute path The full location of the file or directory.
  2. Relative path The location of the file or directory relative to the current directory.

Absolute Path

/ (root)
├── home
│   └── user
│       ├── documents
│       │   ├── course
│       │   │   ├── THIS_RMD.rmd
│       │   │   └── myData.csv
│       │   └── notes.txt
│       └── downloads
|           └── IMAGE.png
└── var

Absolute Path

An absolute path starts from the root directory (/) and specifies the full path to a file or directory.

  • Path to THIS_RMD.rmd: /home/user/documents/course/THIS_RMD.rmd
  • Path to IMAGE.png: /home/user/downloads/IMAGE.png

Reminder for Windows system

  • You will find your file path using \ instead of / in your file explorer. In R, you need to transform these \s to / if you directly copy and paste the file path.
  • You need to indicate your drive, so it usually looks like C:\Users\Ray\Blah\Blah

Relative path

/ (root)
├── home
│   └── user
│       ├── documents
│       │   ├── course
│       │   │   ├── THIS_RMD.rmd
│       │   │   └── myData.csv
│       │   └── notes.txt
│       └── downloads
|           └── IMAGE.png
└── var

Relative Path

A relative path is defined relative to the current working directory.

  • getwd() to get current working directory

  • setwd('A PATH') to set new working directory

  • ./ refers to the current directory,

  • ../ means “go up one directory level”.

Assume that we are working on THIS_RMD.rmd, so the current directory is /home/user/documents/course.

  • Path to myData.csv : ./myData.csv or myData.csv
  • Path to IMAGE.png: ../../downloads/IMAGE.png

Mondrian, Piet. 1935. Composition (No. 1) Gray-Red. Oil on canvas. the Art Institute of Chicago.