Before beginning, you will need to download a collection of R packages called the tidyverse. The tidyverse can be installed and utilized using the following two lines of code:
A package is a collection of functions, data, and documentation that extends the basic capabilities of R. Packages are the key to successfully using R.
- Packages should always be loaded at the top of the script.
- Spend a little time ensuring that your code is easy to read.
- Use concise variable names
- Annotate what you're doing, it makes it easier to reproduce what you've created and to find and fix any problems with the code.
Reading in Files
In order to perform any actions on experimental data, you must be able to import the file into R. First, you will want to get your working directory, which is where files can be imported and exported from RStudio.
In the console, it should print something like this, only with the information from whatever computer you are working from.
Once you know your working directory, you can change it to a more convenient pathway using:
To use this function, you will have to set the pathway for R to follow.
Above is just an example of how to code for setting a working directory pathway. There are lots of examples online that can show you alternate pathways. There are a couple of easier ways to change a working directory, though. The first is using the Tools | Change Working Directory menu on Windows or Session | Set Working Directory on Mac. You can then click through to open the folder to use for your project.
On a Mac, you can also drag and drop a file from Finder onto the RStudio Dock icon which will cause RStudio to startup with the folder as the working directory.
Choose a working directory at the beginning of your project and then do not change it. Any file references will be made invalid if you change your working directory after you have begun.
Now that you have a working directory, you can begin to import the files to manipulate and analyze withing RStudio. Below are some of the most common methods of reading the files into your project.
When importing data into RStudio, always create an object for it in the global environment. Below is an example of an object named "mydata".
mydata <- read.csv()
There are packages that can be downloaded to read in Excel files, but for the sake of this tutorial, we are going to convert all excel files to .csv files. To do this, you will choose "Save As" and then select the .csv extension. A good tip is to save the original Excel file and then make a copy in .csv form. Make sure to save the new .csv to the folder that is your working directory!