Lecture Notes on Importing Data into R

Jul 29, 2024

Importing Data into R

Introduction

  • Focus of today’s session: Importing data correctly into R via RStudio.

Getting Started with RStudio

  • Console: Located at the bottom left.
    • Blink cursor: You can type code directly here and hit return.
  • Script Editor: Preferred for writing and saving code and notes.
    • Use # to write comments/notes in code.

Setting Your Working Directory

  • First step for every session: Specify the working directory.
    • Select folder (e.g., r_examples).
    • Confirm it in the console.

Overview of the Data

  • Data file to import: serial.csv
  • Contains five columns with both categorical and numerical data.
    • Important: Data must not have special characters or spaces in column names.
      • Example: Use fruit_loops instead of fruit loops to avoid confusions.
  • Ensure the dataset is saved in CSV format.

Importing the Data

  • Naming the Dataset:
    • Choose a descriptive name (e.g., be_fast).
  • Code for Import:
    • Use the read.csv() function:
      be_fast <- read.csv("serial.csv", header = TRUE, sep = ",")
      
    • Parameters:
      • header = TRUE: Indicates the first row contains headers.
      • sep = ",": Confirms data is comma-separated.

Checking Imported Data

  • Use be_fast in the console to view the data or double-click in the global environment to confirm data is imported correctly.
  • Data structure should resemble an Excel sheet.

Conclusion

  • Successful completion of the first part of the session.
  • Next session: Basic descriptive statistics and saving scripts.