- Tip: Cheatsheet for manipulating data in dplyr package
- Tip: What are all the files in the Files Tab?
- Tip: How to run lines of code?
- Tip: Installing packages
- Tip: Commenting with hash
- Tip: Finding and setting a working directory
- Tip: Filepaths in R
- Tip: Watch out for quotation marks:
"are not the same as
‘ ’ “and
- Tip: Cheatsheets
- Tip: If RStudio Hangs
- Tip: How to make descriptive tables for different levels of a variable
- Useful: Code to clean up labels on the crime (lga) dataset
Tip: Cheatsheet for manipulating data in dplyr package
Note useful functions like:
Tip: What are all the files in the Files Tab?
Tip: How to run lines of code?
We run lines of code in this script file by highlighting them and then either:
Tip: Getting help
Google: Often the easiest way to work out how to do something in R is to Google for it. Just type it out as a question ending in “in r”. E.g. how do I delete a variable in r
R helpfiles: These are very good for looking for exact commands and arguments for functions. You can search it by just going to the Help Tab and typing your command in the top right corner. Alternative you can type
Tip: Installing packages
One of the most important features of R is the large number of extra functionality that has been written by users across the world. This extra functionality is contained within packages (which is basically synonymous with the word ‘library’).
To run any function from a package, there are two main rules to remember:
For example, say we wanted to import a dataset that was in excel format. We would need the package ‘readxl’. If we have never used ‘readxl’ before then first we need to install the package.
If you run this command in RStudio, you should get a large amount of output that looks something like this:
You will know when R has finished running your code, because in the console window the “>” character will appear.
When I ran the command on my own computer, the output was as little different to what is shown above. What output you get will dependent on which other packages you already have installed in R. You might see something like this, with the last few lines of output to the console window being:
Before you can run the package, you also need to load the package. To do this you use the ‘library()’ command.
Notice that the library command does not have any inverted commas, while the install packages command does. This is an annoying quirk of R which you need to be aware of.
Tip: Commenting with hash
In most programming languages there is a symbol to tell the program to ignore the rest of the line. In R the comment character is hash:
This is useful for
As an example of (1) you might write a few lines of code at the top of your R script file:
As an example of (2), you might put a # in front of the install.packages(“readxl”) command from the previous section and then try to run the whole line.
Nothing will happen. You will just return to the “>” character in the console window.
In general I think this is good practice: once you have installed a package, I suggest that you ‘comment out’ the code for that line, so it won’t run again. Installing packages takes time, and you don’t want to do this everytime you run your code
However the code will be there if you ever need to run the code again on a different computer (such as if you give the code to a colleague), or if you reinstall R, when you update R.
Tip: Finding and setting a working directory
Getting your Working Directory: R will always have a directory it looks into first when you ask it to load a dataset or save a file. This is called the ‘Working Directory’.
The advantage of the working directory is that you don’t need to type out the full file path for every file you access.
However, often we aren’t sure what our working directory is, or we want to change it.
If you ever want to know what your working directory is, then you can use the command
If you want to change your working directory, the command is
Tip: Filepaths in R
One thing to note in R is that the slashes between folders aren’t the same as those in Windows.
In R, you can use one of two conventions for separating directories:
But you cannot use the traditional windows format of a single back slash, i.e. "", such as
Finding your filepath in Windows: To find your file path right click on the file or folder in Windows Explorer. You should see a menu. Click “Properties”vThe location of the file will be reflected in the “Location” row. You can highlight the whole location and right click to copy it.
When you copy this path back into R, remember to either: (1) add a second forward slash; or (2) change the forward slashes to back slashes. And also remember to include double quotation marks.
Finding your filepath in Mac: Navigate to your file in Finder. Right-click the file. A menu will open up. In the menu, click on “Get Info”. Highlight the text after “Where”. Right click in the highlighted area. In the menu that opens up, click on Copy.
When you copy this path back into R, remember to make sure between the folders there is either: (1) two forward slashes; or (2) one back slash. And also remember to include double quotation marks.
Tip: Watch out for quotation marks:
In typesetting there is a distinction between straight quotes (such as
R uses straight quotes, such as
Microsoft Word uses curly/smart quotes, such as
If you copy over code that contains quotation marks directly from a website, and the code doesn’t work, delete the copied quotation marks and replace them within RStudio by retyping them.
Microsoft Word’s quotation marks are not compatible with R.
If you are ever looking for how to use RStudio or R, these are a few really helpful ‘cheatsheets’.
The full list of RStudio Cheatsheets can be found here.
Tip: If RStudio Hangs
If RStudio hangs and takes a long time. look at the top right of the console window and check if there is a STOP sign displayed in Red. This means that a command is still running. You can press the STOP sign to stop a command.
If you don’t see the STOP sign, or pressing it does nothing, then your other option is to force close RStudio. To do this:
Tip: How to make descriptive tables for different levels of a variable
Useful: Code to clean up labels on the crime (lga) dataset
Removes the words “(Rate per 100,000 population)” from variable labels