Skip to main content
Social Sci LibreTexts

2. Prerequisites and Reading in Files

  • Page ID
    33764
  • Prerequisites

    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:

    install.packages("tidyverse")
    library(tidyverse)
    

    Definition: Package

    package is a collection of functions, data, and documentation that extends the basic capabilities of R. Packages are the key to successfully using R. 

    Pro-Tips

    • 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. 

    Screen Shot 2019-11-06 at 10.37.20 AM.png

    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.

    getwd()
    

    In the console, it should print something like this, only with the information from whatever computer you are working from.

    Screen Shot 2019-12-02 at 10.36.51 AM.png

    Once you know your working directory, you can change it to a more convenient pathway using:

    setwd()
    

    To use this function, you will have to set the pathway for R to follow.

    setwd("/Users/home/Desktop/libretext")
    

    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.

     Screen Shot 2019-12-02 at 11.15.05 AM.png

    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.

    TIP:

    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. 

     

    Importing Files

    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.

    tIP:

    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()
    
    

    CSV

    read.csv()
    
    

    Excel

    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!

    Screen Shot 2019-12-02 at 12.13.07 PM.png

     

    • Was this article helpful?