This is a bonus WPA that you can use as revision on controlling your working directory. It is entirely voluntary and I suggest you complete it after completing WPA8 or in your own time outside class. Your working directory is the folder that R (and Rstudio) will search whenever you direct it to load from (or save to) your machine to (from) R.
I previously told you two methods for controlling your working directory. These were using R projects (available in Rstudio) or by using setwd() to set the filepath to your desired working directory. This WPA will have 3 sections. The first will go through how to use setwd(). The second will go through how to create an R project, and how to create a new script in an existing R project. These two sections/methods are independent of each other. You can do whichever method you prefer, or you can try out both methods to see which is easier for you.
Section 3 will then go through some examples for saving and loading data.
I’ve put up two versions of this WPA, one with worked answers and instructions and one without. You can use whichever one you prefer. If you want to test your ability I’d suggest that you use the version without answers first. If you are struggling with the concept or practicalities of setting your working directory, I’d suggest you use the worked answer version and ask me lots of questions.
Before doing anything in R the first thing I want you to do is to create a new folder, called Week10. It doesn’t matter where you create this folder (although I’d recommend using a similar location to the rest of your coursework), but you do need to know where this folder is. In the Week10 folder create two subfolders called WDrevision and otherfolder. We will use the WDrevision folder as our working directory for both methods.
Open Rstudio and create a new R script called wpa_setwd.R.
Check the current working directory.
Change the working directory, using setwd so that it is otherfolder. Check the working directory again.
Change the working directory, using setwd so that it is WDrevision. Check the working directory again.
Use the following code to create some fake data. Then save this data to the folder WDrevision. Call the saved file dataswd.Rdata
data1<- array(1:10, dim=c(2,5))
Did it save in the correct location (Look in the folder).
Save the script and close Rstudio.
Open Rstudio and create a new R rpoject using the menus. Make the location of the R project the existing working directory/folder WDrevision. If you look in the WDrevision folder you should see a file called WDrevision.Rproj. This is your R project.
Create a new R script called wpa_create.R.
Check the current working directory. If it isn’t WDrevision you have made a mistake when creating the project.
Use the following code to create some fake data. Then save this data to the folder WDrevision. Call the saved file datarp.Rdata
data2<- array(0, dim=c(2,5))
Did it save in the correct location (Look in the folder).
Save the script and close Rstudio.
R projects are great for organising your analysis, particularly when you have a lot of script files for the same project. This is because you can have multiple R scripts in a single project. All you need to do is insure that you open your desired R project first, before creating any new script files. For instance you could have the WPA script for each week all saved as part of the same R project.
Open up the R project you just created WDrevision.Rproj.
Create a new R script called wpa_exist.R.
Check the current working directory. If it isn’t WDrevision you aren’t in the R project (Check if you have multiple sessions of Rstudio open)
Use the following code to create some fake data. Then save this data to the folder WDrevision. Call the saved file datarp.Rdata
data3<- array(1, dim=c(2,5))
Did it save in the correct location (Look in the folder).
Save the script and close Rstudio.
This section assumes you have completed at least one of Sections 1 and 2. I will use data1 and dataswd.Rdata in the questions and examples, but you can use data2 and datarp.Rdata if you did not complete Section 1.
WDrevision (use whichever method you prefer).Load data1.
Create a new dataset, called data4, by subracting 10 from each score in data1.
Use the following code to save data4.
save(data4, file= "data/data4.Rdata")
The code should produce an error. Why? How can you fix it.
Lets say that instead you wanted to save data4 in the folder otherfolder. You could do this by changing the working directory. However, this is inefficient, as you would need to change it back after to keep working. Putting ../ at the start of a filepath goes to the parent directory. Try to use this to save data4 in otherfolder.
Check your working directory is still correct.
When loading data directly from the internet, our working directory doesn’t really matter. Instead of giving the filepath and name, we provide the URL for the location of the data. For example we use the following code to load the fake data from wpa6.
wpa6.df <- read.table(file = "http://nathanieldphillips.com/wp-content/uploads/2016/11/wpa6.txt",
header = TRUE,
sep = "\t")
When loading .Rdata files, we are directly loading a filetype created by R, so we don’t need to provide any information about the data format. When we load .txt files using read.table (or other non-R file types) we need to provide some additional details about the format of the data. This section deals with 2 common mistakes I’ve seen students make.
wpa6.df2wpa6.df2 <- read.table(file = "http://nathanieldphillips.com/wp-content/uploads/2016/11/wpa6.txt",
header = FALSE,
sep = "\t")
What is different between wpa6.df and `wpa6.df2. Use head, str and/or names to compare the two. What mistake have we made in wpa6.df2
Use the following code to load another version of the student.math data. We’ll call it wpa6.df3
wpa6.df3 <- read.table(file = "http://nathanieldphillips.com/wp-content/uploads/2016/11/wpa6.txt",
header = TRUE,
sep = ",")
wpa6.df and wpa6.df3. Use head, str and/or names to compare the two. What mistake have we made in wpa6.df3You don’t need to submit this WPA. But you should check your answers