The Directory and .RData Files

The Directory

To watch a video about the R directory, click below. Otherwise, read on!

The folder that you are working in in R is called the Directory. The directory is where you will be able to load files.  To see what files, shortcuts, and other things are in your directory, type the function dir(). That function will show you the other files in your folder. To change which directory you are working in for Windows:

  1. Click on File in the top right hand corner
  2. Click on Change Directory…
  3. Pick the folder you wish to use as your directory

To work with a file in R, you must have it in your directory.  For instance, you cannot load an Excel file into R if your directory is “Folder 1” and the Excel file is in “Folder 2”.

R Files

Watch a video on .RData files, the native file for R. Otherwise, read on!

Many applications save their work by means of a special file. For example, Microsoft Word save their files as .doc or more recently as .docx. Microsoft Excel has a different file format called .xls or more recently .xlsx. Microsoft PowerPoint, SAS, MiniTab, Stata and other computer programs have the ability to read and/or write different types of files. R can read in many different files, but it has its own native file type as well. We will discuss how to read different file types at a later point. But for now we will just focus on R’s file type, .RData. You can create and load .Rda files very easily. We are assuming that you are in your working directory of desire. To create a .Rda file, simply use the save() function. Below is example code to perform this.

#Creating test.RData

x<-c(1,2,3)

y<-c('a','b','c')

save(x,y, file<-"test.RData") #notice how you can put two different objects together in this format
							  #we can put them together no matter if they are characters or numbers


#creating testW.RData
W<-c(7, 8, 9)

save(W, file='testW.RData') #alternativly, we can just put one object into the .RData file


To load a .RData file, simply use the load() function. Below is example code to perform this using the test.RData file we just created.

load('test.RData')

ls()#this will list all the object we currently available
	#notice that we have x and y available now
	
x

y

load('testW.RData')

ls()#w is now an object we can call

w