R: Using RStudio and Importing Data

Where do you start?

Learning a new program can be scary and overwhelming at times.  So let me share a few of the shortcuts I’ve learned these past few months on using RStudio.

Navigating the Windows in RStudio

When you open RStudio you’ll see 4 windows or 4 sections on your screen:  editor, console, history, and environments with tabs.  Let’s start with the environments window – you should see 6 tabs:  Environment, Files, Plots, Packages, Help, and Viewer.   The Environment tab lists the files/datasets that are being used during the current project.  The Files tab allows you to view all the files that are available in your working directory.  The Plots tab will show any plots that are created during your session.  The Packages tab will list all packages that you have loaded.  The Help tab is self-explanatory.  A quick sidenote, the Help window is great!  Please take advantage of it by using the search function in the Help tab.

The History window will list all the lines of code that you have run until you clear it out.  A great way to see what you have done – especially if you encounter troubles along the way.

That leaves the editor and the console.  The editor is where you open an R script file and the console is where you run your code as you type it in.  To run code that is in your editor – select the bits of code and hit Ctrl-Enter to run it.  In the console, you type the line, hit enter and it runs immediately.  I use these two windows in tandem.  To move between these two windows – Ctrl-2 moves you to the Console window and Ctrl-1 brings you back to the editor window.  Of course, a mouse works great too!

One more quick tip – the console window can fill up quite quickly and to me, can feel very cluttered.  Remember the History window will keep a history of your code, so it would be ok to clear out the console as you see fit.  In order to do this, use Ctrl-L to clear it out.

Working Directory

Sometimes having your program always refer to the same directory, when saving files or when opening files, can be very handy.  You’ll always know where your files are!  R makes it very easy to accomplish it.

First, let’s see what the current working directory of your RStudio is by typing:

getwd()

To change the working directory for the current project you are working on type:

setwd (“C:/Users/edwardsm/Documents/Workshops/R”)

Of course, you’ll want to make this a directory on your computer 😉   But as you look at this – do you notice anything odd about this statement???  You’ll notice that the slashes / are the opposite direction than you normally see on a Windows machine.  Changing these manually can be a time consuming effort.  One way around this is to add an extra \ after everyone in your location.  See below:

setwd (“C:\\Users\\edwardsm\\Documents\\Workshops\\R”)

Always double-check your working directory by checking getwd() Are the results what you were expecting?  If not, try it again.

More ways to set your working directory:

  • In RStudio, Session in the File Menu provides 3 options for setting your working directory:
    • To Source File location (the directory where you save your R script and program files)
    • To Files Pane Location – in the Files Pane – navigate to the location you want to have as your Working Directory.  Once you have it selected in the Files Pane, then choose Session -> Set Working Directory -> Files Pane location.  You will see the new working directory appear in your console and it should match what you select in the Files Pane.
    • Choose Directory – will open a windows dialogue box where you navigate and select the directory of choice.
  • While you are in the Files Pane location – navigate to the directory that you would like to set as your working directory, then in the Files Pane – select More -> Set Working Directory.  This option is very similar to the Files Pane Location option under the Session menu of RStudio.

Importing Data

Every statistical package has a number of ways to bring data in.  R is no different!  Now most of us will use Excel to enter our data, then we’ll clean it up in Excel before we import it into our preferred statistical package.  For the purposes of this session, I will use an Excel worksheet as an example.

The first step is to save our Excel worksheet as a CSV (Comma separated values) file.   In Excel, File -> Save As -> Select CSV as the file format.  You will be asked several questions regarding the format of the file you want to save.  Please note that when you save an Excel file or a worksheet as a CSV, it will only save the worksheet that you have selected and NOT the entire Excel file (which may have several worksheets).

There are a couple of ways to import the CSV file.  But, the first thing you’ll need to do is give the file a name.  Please note that in R, you can use a “.” in the name of the file.  For more information on best practices for filenames and variables, please visit the Best Practices for entering your Research Data using Excel

The following code will import a CSV file called Example and save it in an R file called my.data .  For this piece of code, the file, Example.csv, must be in the working directory that you’ve set earlier  OR you will need to provide R with the full location of the file – this includes the drive and directory structure.  The header=TRUE option, let’s R know that the first line of the datafile has a header or contains the names of the variables.

my.data=read.csv(“Example.csv”, header=TRUE)

If you have files that are not in the working directory or you don’t want to provide the full location of the file, then you can use the following piece of code.  Personally, I much prefer this next piece.

my.data2=read.table(file.choose(), header=TRUE, sep=”,”)

Now my file in R will be called my.data2  .  Using this code, a dialogue box will open and will allow me to navigate to the directory that holds my files.  This coding option provides me more opportunities than the first, in my opinion.  The header=TRUE holds the same meaning as above.  However, this time we need to specify the delimiter, or the item that is separating the variables in the CSV file, which is a comma – depicted as sep=”,” in the code.

Why do you have to use a sep=”,” option in the second case and not in the first, we are reading the same CSV file in both cases?  The first import coding option is using a function called read.csv – so R already knows that it will be reading a CSV or comma separated file.  Whereas in the second case, read.table – R has no idea what type of data is till be reading, therefore we need to specify what the delimiter or separator is in the datafile.

There are other ways to import data into R, but I have found these two, with preference for the second one, to be quite direct and straightforward.  It also encourages the user to maintain a data Master file in Excel, with a text copy of your data to use for the analysis.  Remember the text format will be a great option for preserving and sharing once your project is complete.

Two other packages discussed during our session today, that import data, in one case specifically Excel files and in the second case, many data formats.  These are:

  • read.xl package
  • tidyverse package

Look to future sessions on more details about these packages.

If you have suggestions or hints for other methods to import data into R, please leave a comment below or send me an email and I can add them here.

ARCHIVE: Communities of Practice: Coming Fall 2017

“Communities of practice are groups of people who share a concern or a passion for something they do and learn how to do it better as they interact regularly.”  – wenger-trayner.com

The OAC Stats Support Service will facilitate Communities of Practice (COP) to engage the OAC research community and assist with the statistical analyses and statistical software. Our researchers use a variety of statistical approaches and statistical software packages to conduct their research, by meeting, sharing perspectives, and learning new aspects of our software and/or statistical approaches, as a community, we can create enriched learning environments for all.

Fall 2017, will see the creation and revitilization of four COPs:

  • SASsy Fridays
  • Crimes of Statistics
  • OAC R Users Group
  • OAC Data Visualization

SASsy Fridays

SASsy Fridays started as a COP in W14 in response to the growing interest of SAS-specific topics beyond what was being taught in the workshops. If you use SAS and are interested in learning and sharing new approaches to using the software or new statistical approaches in SAS, this is the COP for you!  For past topics please review the SASsy Fridays blog.  If you have a topic you would like to present or would like more information about, please email oacstats@uoguelph.ca. SASsy Fridays sessions will take place in the Crop Science Lab Rm 121A on the following dates and times:

  • Friday, October 13  from 12:30-1:20 p.m.
  • Friday, October 27 from 12:30-1:20 p.m.
  • Friday, November 10 from 12:30-1:20 p.m.
  • Friday, November 24 from 12:30-1:20 p.m.
  • Friday, December 8 from 12:30-1:20 p.m.

Crimes of Statistics

Many of us conduct experiments and run the appropriate statistical analysis, but sometimes we can get caught up in questioning the basics of the theoretical background. Topics such as replication, sampling, power, p-values, and many more. This COP will meet to discuss these and other topics. A short presentation on the topic du jour will be followed by a discussion of situations you may have encountered. The Crimes of Statistics COP will meet in the OAC Boardroom (Johnston Hall) on the following dates and times:

  • Tuesday, August 22 from 10:00-10:50 a.m.
  • Tuesday, September 5 from 10:00-10:50 a.m.
  • Tuesday, October 3 from 10:00-10:50 a.m.
  • Thursday, November 2 from 10:00-10:50 a.m.
  • Thursday, November 30 from 10:00-10:50 a.m.
  • Tuesday, December 12 from 10:00-10:50 a.m.

The first meeting on August 22 will be an information gathering session. Please bring any topics you would like to see discussed to this session.

OAC R Users Group

R is growing in popularity and is gaining international acceptance in the research community. The goal of this group will be to exchange knowledge about R-packages and R-libraries that your research field or your lab uses. A short presentation or demonstration of  practical application of an R-package or R-library will be followed by questions and exploration of other uses for the presented material. The OAC R User Group meetings will take place in Crop Science Lab Rm 121A on the following dates and times:

  • Friday, October 20 from 12:30-1:20 p.m.
  • Friday, November 3 from 12:30-1:20 p.m.
  • Friday, November 17 from 12:30-1:20 p.m.
  • Friday, December 1 from 12:30-1:20 p.m.
  • Friday, December 15 from 12:30-1:20 p.m.

Data Visualization

You have been collecting data for a project and now it’s time to do something with it! What do you do? How do you present it? Should it be a table? A graph? A chart? This COP will discuss different ways of presenting data, the pros and cons of different formats, and will encourage the community to demonstrate their favourite data visualization formats. The Data Visualization COP will meet in the OAC Boardroom (Johnston Hall) on the following dates and times:

  • Tuesday, October 17 from 12:00-12:50 p.m.
  • Tuesday, October 31 from 12:00-12:50 p.m.
  • Tuesday, November 14 from 12:00-12:50 p.m.
  • Tuesday, November 28 from 12:00-12:50 p.m.
  • Tuesday, December 12 from 12:00-12:50 p.m.