There are two main goals of this first homework: 1) For you to download R and RStudio so that you are set up for all future homework and labs and 2) To introduce R and RStudio with some basic exploratory work.
If you have any trouble with either of these tasks, please go to a TA’s office hours for help. It is crucial that you are properly set up for this class by the time the first homework is due so that you do not fall behind.
Throughout this class, homework will be uploaded onto Blackboard. For Homework 1 the following should be uploaded onto Blackboard:
Your LastF_HW1-17.Rmd file where LastF is replaced by your last name and the first letter of your first name.
Your LastF_HW1-17.pdf file
In this homework, you will be given instructions on how to create these files. For information on how to upload files to blackboard see http://bbhelp.cit.cornell.edu/support/help-for-students/.
In this class, we will be downloading and uploading data and files from Blackboard and possibly other sources. It is important that you know where these files are located. Here we will create a folder for BTRY 3010 on your computer.
The instructions below are for students who have personal computers that they will use for this course. If you are going to use lab computers, create a “BTRY3010” folder on your thumb drive. Then within this folder, create a folder for homework 1. It is best if every week you create new folders to store all of the files for the homework and lab for that week.
Please complete the following tasks using the appropriate instructions for your computer (i.e. Mac or Windows).
For Macs: Every file you have on your computer can be referenced by a file path. If you open Finder which should be on your toolbar. It will look something like this, where your home directory will be labeled differently than the one shown here and you may have different folders listed.
All of your computer files can be accessed through Finder. The home directory here is called “cecilia”. In this directory is found a folder labeled “Documents”. For most of you, you likely have your “Documents” folder under your home directory. Generally you will also find a shortcut to your “Documents” folder in the list of folders under “FAVORITES” on the left side of the Finder window.
Once you have found your “Documents” folder, create a new folder within “Documents” called “BTRY3010.” This is done by double clicking on the folder “Documents” to open it and choosing File -> New Folder from the menu bar. Rename this folder as “BTRY3010”.
We suggest you create a new folder for every assignment in this class where all the files for that assignment are kept. Use the directions in step 2 to create a folder for homework 1 in your “BTRY3010” folder called “HW1”.
On the left hand side of your Finder window, the folder “Downloads” should be listed. If not, it should be found under your home directory. This is an important folder as everything you download will be put in that folder. Now open your internet browser. We suggest that you use Chrome, Firefox, or Safari.
In Blackboard, go to the folder for homework 1 and right click on the HW1-17.Rmd file. Choose “Download Linked File.” This will send this file to your “Downloads” folder.
Now put HW1-17.Rmd in your homework 1 folder. You can do this by opening two finder windows. A second Finder window can be opened by choosing File -> New Finder Window from the menu bar for Finder. Open one Finder window to “Downloads”. Open the other Finder window to your folder for homework 1. Click on the HW1-17.Rmd file in “Downloads” and drag it into the folder for homework 1. Rename this file as “LastF_HW1-17.Rmd“.
Proceed to the instructions for Macs to download R and RStudio.
For Windows: Every file you have on your computer can be referenced by a file path.
Clicking on this icon gives you access to the “Documents” folder on your computer.
After the folder is created, change the name of this folder to “BTRY3010.” There is a couple of ways to do this. One way, is to right click on New Folder and one of the options you will be given is Rename.
Click on your BTRY3010 folder. For every assignment, we suggest you create a new folder for that assignment within your BTRY3010 folder where all the files for that assignment are kept. Use the directions in steps 2 and 3 to create a folder for homework 1 called “HW1” in your BTRY3010 folder.
Note that in the window containing your “Documents” folder, on the left hand side are a list of other folders that you can also access. One of these folders is called “Downloads”. Anything you download off of the internet will be placed in this folder unless you specify otherwise. We recommend using Explorer or Firefox as your browser.
In Blackboard, go to the folder for homework 1 and right click on the HW1-17.Rmd file. Choose Save target as… or Save link as... A window will pop up in which you can specify where this file is saved. If you are not already in the folder you created for homework 1, select “Documents” from the left hand side of this window. Then double click on the BTRY3010 folder you created within “Documents”, and finally double click on the HW1 folder that you created within your BTRY3010 folder. Change the name of this .Rmd file to “LastF_HW1-17.Rmd" before you save it. Save this document in your folder for homework 1 by clicking on “Save” at the bottom of this window.
Proceed to the instructions for Windows to download R and RStudio.
In this class we will primarily be using RStudio for data analysis. RStudio is an convenient interface for the R programming language. Here we will go through the steps to download R and RStudio. Note: If you already have these applications, please download the latest versions so that RStudio has the same look and functionality for everyone.
For Macs:
We will first download R. Go to the wesite, http://cran.r-project.org/bin/macosx.
Click on either R-3.4.1.pkg or R-3.3.3-.pkg or R-3.2.1-snowleopard.pkg depending on your operating system; see descriptions. If you are unsure which operating system your computer is using, click on the apple icon on the top left of your computer screen. Then select About This Mac. The version of your operating system can be found on the window that pops up. Clicking on the appropriate link for your operating system will download the R package onto your computer. It will take a few minutes or significantly more depending on your downloading speed.
Go to your downloads folder and double click on the package you downloaded. This will start the installer for R. Go through the steps to start the installation. When you have choices, unless you prefer otherwise, choose the defaults. This will take a couple of minutes.
R should now be in your “Applications” folder which can again be found on the left hand side of your Finder window.
Next, we will download RStudio, our main tool for this class. Go to the website http://www.rstudio.com/products/rstudio/download/.
At the bottom of the page, click on RStudio 1.0.153 - Mac OS X 10.6+ (64-bit). This will download RStudio and a folder will pop up that includes both the “Applications” folder and RStudio. Drag RStudio into the “Applications” folder and close this window.
Likely after step 6, you will have an RStudio icon on your desktop. You can right click on this icon and choose Eject to get rid of this icon.
You are now ready to use RStudio.
For Windows:
We will first download R. Go to the wesite, http://cran.r-project.org/bin/windows/base/.
Click on Download R-3.4.1 for Windows. At the bottom of the Explorer window, you will be asked whether you want to run or save this file. You can go ahead and run it unless you would like to start the installation at a later time. After selecting to run this program, you may be asked if you want this program to make changes to your computer. Click “Yes.”
This will start the installer for R. Go through the steps to start the installation. When you have choices, unless you prefer otherwise, choose the defaults. This will take a few minutes.
If you click on the “Start” icon for Windows,
you should find an alphabetical list of your applications. R should now be listed as one of these applications, and you can access it here.
Finally, we will download RStudio, our main tool for this class. Go to the website http://www.rstudio.com/products/rstudio/download/.
At the bottom of the page, click on RStudio 1.0.153 - Windows Vista/7/8/10. Again, at the bottom of the Explorer window you will be asked whether you want to run or save this file. Go ahead and run it. You will likely be asked if you want this program to make changes to your computer. Choose, “Yes.”
This will start an installer for RStudio. Unless you prefer otherwise, choose the defaults for the installation process. This will take a few minutes.
Once RStudio is installed, it also should be found in your application files.
You are now ready to use RStudio.
The goal of this homework is to introduce you to R and RStudio, which you’ll be using throughout the course both to learn the statistical concepts discussed in the texbook and also to analyze real data and come to informed conclusions. To straighten out which is which: R is the name of the programming language itself and RStudio is a convenient environment for writing and running R code.
As the course progresses, you are encouraged to explore beyond what the labs and homework dictate; a willingness to experiment will make you a much better programmer. Before we get to that stage, however, you need to build some basic fluency in R. Today we begin with the fundamental building blocks of R and RStudio: the interface and basic commands.
The panel in the upper right contains your workspace as well as a history of the commands that you’ve previously entered. Any plots that you generate will show up in the panel in the lower right corner.
The panel on the left is where the action happens. It’s called the console. Everytime you launch RStudio, it will have the same text at the top of the console telling you the version of R that you’re running. Below that information is the prompt, >. As its name suggests, this prompt is really a request, a request for a command. Initially, interacting with R is all about typing commands and interpreting the output. These commands and their syntax have evolved over decades (literally) and now provide what many users feel is a fairly natural way to access data and organize, describe, and invoke statistical computations.
If you have used R before, the console in RStudio has the same functionality as the basic R console. Any command in R can be used in the exact same way in the RStudio console. RStudio, however, is designed to be more user friendly and to provide an easy way to reproduce any previous work in R through R Markdown. In this section of your homework, you will produce your first homework file through R Markdown.
Complete the following steps to create your first homework documents.
For this homework, you will modify the previously created HW1-17.Rmd document. If you completed the steps above correctly, this file should now be named “LastF_HW1-17.Rmd" and be found in your folder “HW1”.
In RStudio, in the lower right window click on the tab “Files.” In “Files” navigate to your .Rmd file for homework 1. Click the highlighted name of this file to open it in RStudio. This file will now be opened in RStudio in a new window on the left above your R Console.
Click on the window of your .Rmd file. You can edit this file. In lines 12 and 13 of this file, put in your name and your NetID.
R Markdown has the capability to create (or “knit”) .html, .pdf, and Word documents from a .Rmd file. However, without a LateX editor you cannot knit pdfs. On the menu for your .Rmd file, click the arrow next to . This dropdown menu allows you to choose whether you want to knit a pdf, html, or Word document.
If you have a newer operating system for Mac or Microsoft Word, you will end up with a nice looking document that you could eventually save as a pdf and submit.
If you use a Mac with an older operating system, this Word document may not look very nice. If so, you have a couple of options:
You may want to download a LateX editor such as MiKTeX for Windows or MacTeX for Macs. With this extra application you can “knit” your .Rmd file directly into a nice looking pdf. If you think you want to use this option, see Dr. Earls first before downloading a LaTeX editor.
The computers in Mann library lab B30A have MiKTeX installed on them. You can wait to run your .Rmd document until right before you submit your homework, and “knit” the pdf in that lab. However, this option requires some advance planning since the labs are only open certain hours.
If you were able to “knit” a Word or pdf document successfully it will now be saved as a .docx or .pdf file with the same name as your homework file with the new extension. By default it is saved in the same folder as the .Rmd file from which it was generated. Look in your folder for your first homework using the lower right window of RStudio to verify your .docx (or .pdf) homework file is now there. This .docx (or .pdf) file is saved as the newest version every time you re-knit your Word document.
For this and future homework, you will submit your updated .Rmd file (where it has been updated exactly as directed) and a pdf of the knitted .Rmd file.
R is an application primarily used for data analysis. To use R, you need to use the proper syntax for coding in R. In this class we do not assume prior knowledge of this syntax, and you will be given a lot of code that you only need to run. In lab, you may be given code to use for a particular dataset that you can edit for use in your homework when applying this code to a different dataset.
Many “libraries” or “packages” have been developed for R. These libraries contain functions that can be used directly. For example, the function plot(x,y) will plot points on a graph where the domain and range have been user defined.
You will not have all packages for R installed on your computer, but many basic functions are available after an initial installation of R. If we need more functions as the semester progresses, you will be told how to download and access these functions.
For the rest of homework 1, follow the instructions in your .Rmd file for homework 1.