The Basics of R

The Command Console

When you first open R what you will see is the command console.  Here we can enter commands, and view their output.  We enter commands at the command prompt (>):

> Type in a command, then press ENTER to evaluate it
If we try to evaluate the text above we will get an error, since none of that text is an actual command.  We can also include comments, text that is not evaluated as a command.  To include a comment, precede all text with a hash tag (#).

> # This is a comment
Evaluating this command will return no result since R ignores all text following the hash tag.


When you first start R you will see the following text (or something similar) appear on screen:

R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Previously saved workspace restored]
While this is information you might want to know, we don’t need it.  To clear this text from the screen, or to clear your command console at any point, press CTRL + L.

With an empty screen we can now begin entering some basic commands.


Many of the problems we encounter in statistics require us to analyze a data set.  Therefore, our most basic command is creating a list of values.  

Example: The table below lists the number of alcohol-related fatalities in car crashes per year in the United States.  Create a list in R containing each of these values.

2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
17,380 17,400 17,524 17,013 16,919 16,885 15,829 15,387 13,486 12,744
The first thing we need to do is decide on a name for a list.  We can use any name we want, with a few restrictions:

·         The name cannot start with a  numeric character
·         The name cannot have any spaces (separate words with a dot/period)
·         The name cannot have any special characters

I always recommend choosing a name that makes sense given the data we are considering, but you can use any name you want.  Say we want to call this data set alcohol.related.  To construct our list we will enter the following:

> alcohol.related = c(17380, 17400, 17524, 17013, 16919, 16885, 15829, 15387, 13486, 12744)
Notice that the commas have been removed from all numbers as commas are used to separate individual data values.  The letter c essentially means combine all these values into one list.  When we press ENTER and evaluate this command, there will be no output.  The reason is that we did not ask for an output.  We simply created a list.  If we want to view the list we created we simply type in the name we gave to our list.

> alcohol.related
[1] 17380 17400 17524 17013 16919 16885 15829 15387 13486 12744
The [1] at the beginning of the output indicates that the beginning of that row is the first value in our data set.


In addition to the statistical commands we will look at, R can also perform a wide variety of basic mathematical calculations. 

Example: Evaluate 2 + 3(4) – 10 / 5

Most of the key strokes required here are very straight forward.  However, it is important to note that R will not imply multiplication.  Simply typing 3(4) will produce an error.  We need to be sure to type 3 * 4.

> 2 + 3 * 4 - 10 / 5
[1] 12
Example: Evaluate √64 

To evaluate square roots we use the command sqrt.

> sqrt(64)
[1] 8
Example: Evaluate 23

To evaluate exponents we use the “carrot” symbol, which is accessed by pressing SHIFT + 6.

> 2^3
[1] 8
Saving Your Work

There are a few different ways to save your work in R, but if you want to capture all the commands you typed in, as well as the results, I recommend the following.

Click on the File menu, then select Save to File...  This will allow you to save everything that has appeared in the command console as .txt file, which can easily be opened by a number of different programs, including Microsoft Word.



This resource is designed to help you learn the commands necessary for an introductory statistics course.  If you want or need more information about any particular command, though, you can always ask R.  Simply include a question mark (?) before the command to access the R Documentation for that command.

Example: I want to learn more about the t.test command.

> ?t.test
starting httpd help server ... done
Your default web browser will launch, taking you to the documentation page for that command.


No comments:

Post a Comment