Section 1.7 Reading & Writing data
So far, we have created our own data in R or used datasets that are prebuilt in R. Most of the time, you will have data that is not currently in R, but you want/need to bring it into R to do an analysis.
In the case when you have your own data on your computer that you want to bring into R, there are two main ways:
-
read.csv(): Imports a CSV file into R. -
read_xlsx(): Imports an excel file into R (from the packagereadxl, but more about packages later). -
getwd(): Gets the path of the directory you are working in. -
setwd(): Sets the path in your directory that you want to work in.
Below is code to assist with uploading data.
# read.csv example (commented because path varies by machine)
# my_df <- read.csv("/path/to/your/file.csv")
# Working directory helpers
# getwd()
# setwd("/path/where/you/want") # <- avoid hard-coding in projects; we'll cover R Projects later
#setwd("~/Documents/Work/Brooklyn College/Fall 2025")
