What is a working directory in R

The working directory is just a file path on your computer that sets the default location of any files you read into R, or save out of R. In other words, a working directory is like a little flag somewhere on your computer which is tied to a specific analysis project. If you ask R to import a data set from a csv file, or save a data frame as a csv file, it will assume that the file is inside of your working directory. (Source: https://bookdown.org/ndphillips/YaRrr/the-working-directory.html)

Save Your Data

When you will download the AdoptedSamp (2).csv file in your laptop it will be saved in your download folder. We are going to make the download folder your working directory so that R ca read the csv file.

Set R’s working diretory to where the csv file is

You must make sure that R has its working directory set to where this file is located. You need to do this every time need to load a file. (The only exception is if you use RStudio projects).

  1. On the top RStudio menu, click on “Session”,
  2. Then “Set Working Directory”,
  3. Then “choose directory”.
  4. Find out your Downloads folder and open it. It will be set as your working directory and on the console your will see something like this setwd(“~/Downloads”). I am a windows users and in my case setwd(“C:/Users/yrabb/Downloads”) showed up. Now your working directory is set and in the Files window in the right you can see the files in the downloads folder. Now if you want to check what is your working directory you can use following code
getwd()

Now please save your rmarkdown file in the Downloads folder. Use the File menu on the top and then select save as and save it in the Downloads folder You can also check the names of the files in your working directory by using following code

list.files()

Please check the “AdoptedSamp (2).csv” file in the list. If you have lots of files in the working directory, you can search for the file specifically with list.files(pattern = “AdoptedSamp”)

list.files(pattern = "AdoptedSamp")

Loading the Data/ CSV file

CSV files can be read in with the read.csv() function. We are uploading the csv file and saving is as adopt data frame.

adopt= read.csv("AdoptedSamp (2).csv")

Now you will see the adopt data frame in your environment.