Rebekah Robinson, Georgetown College
Ris a statistical software program.
Reasier and more fun to use!
The bottom left panel
Anything entered in the Console will be executed by
Give it a try! Place your cursor in the Console panel, type the following, and then hit return.
The top right panel has two tabs:
Take a look at your History tab. You should see
There isn't anything in your Environment tab because we have not created any objects.
Let's create an expression object! Type the following:
mysum <- 4+9
4+9in the object named
mysum. You could have named it anything!
The bottom right panel has four tabs:
There are 3 important packages to load when you work in
You can load them one of two ways:
require(mosaic) require(tigerstats) require(manipulate)
The top left panel
Let's open an R Script file. Select
An R Script is a file to store code. This allows you to
Once a command is typed into an R script file, it should be run through the Console.
Runbutton at the top right of the Source window.
The most basic way to use
R is as a calculator. Type the following expressions into an RScript and run them through the Console.
5+4 123-45 23*3.7 84/7
R as a calculator, use parentheses to preserve the order of operations. The expression
is different than
R has some built in mathematical functions that should be familiar to you. For example:
sqrt(81) #square root function
cos(pi) #cosine of pi
# sign to add a comment next to a line of code. This helps you remember what a particular line of code does!
What if you couldn't remember what the square root function was? You could access the
help file on this function using either of the following methods.
Tab. Select the appropriate function from the list. Press
F1to open the help file in the Help tab.
To combine values into a list, type
c(1, 3, 5)
It is useful to store a list in an object. Name it whatever you like!
mylist <- c(1, 3, 5) #creates the object mylist #calls the object
 1 3 5
To combine words or letters into a list, type
mywordlist <- c("A", "B", "Cat")
To see the list, type:
 "A" "B" "Cat"
Letters and words must be put in quotations.
To create a list of numbers that are all the same, you can use the concatenate function.
c(2, 2, 2, 2, 2)
It is easier to use the
myreps <- rep(x=2,times=5) myreps
 2 2 2 2 2
rep function requires two inputs:
xis the value that you want to replicate
timesis the number of times you want to replicate
You do not have to enter the names of the inputs
 2 2 2 2 2
as long as you enter them in the correct order.
 5 5
You can also replicate letters or words.
 "apple" "apple" "apple"
seq function comes in handy for making seqences of values.
To create the sequence
1, 2, 3, 4, 5, type
 1 2 3 4 5
This functions requires three inputs:
fromis the starting point of the sequence,
tois the ending point of the sequence,
byis the increment.
This function is useful for other increments.
 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
With replacement means that the volunteer will
Questions to consider before we play:
What is the probability that the volunteer will pull out a red card?
Of the 10 cards drawn, how many do you expect to be red?
Do we think that the volunteer will draw exactly the hypothesized number of red cards?
Questions to consider about our results:
Do these results seem consistent with how many we expected to be red? Or do they seem strange?
Do you still believe your hypothesized probability of drawing a red card? In other words, do you still believe that we are playing with a standard deck?
We will investigate these questions by playing more games. Let's simulate 1000 games in
Let's start by making a deck of cards in
R. Since we only care about the color, we can do this by:
mycards <- c(rep("Red",26),rep("Black",26))
Let's look at the deck:
You should see 26 Reds followed by 26 Blacks.
Now, we will randomly draw 10 cards from our virtual deck.
sample(mycards, size=10, replace=TRUE)
You can take a sample of ten cards and then count up the results:
Black Red 6 4
Let's play 3 games:
do(3)*table(sample(mycards, size=10, replace=TRUE))
Black Red 1 7 3 2 4 6 3 2 8
Black Red 1 7 3 2 4 6 3 2 8
Let's display these results in terms of how many games resulted in a given number of red cards.
Red 0 1 2 3 4 5 6 7 8 9 10 0 0 0 1 0 0 1 0 1 0 0
Now that we know how to read the results, let's simulate 1000 games.
Red 0 1 2 3 4 5 6 7 8 9 10 0 5 41 119 205 249 209 121 42 8 0
Another way to think about these numbers is:
How likely is it that our volunteer drew their original hand, based on our simulations?
A graphical representation of these percents is useful.
Let's shade a bar in the histogram to mark the number of red cards our volunteer drew in the class game.
For example, suppose our volunteer drew 9 red cards.
This shaded region in the histogram represents the estimated chance of drawing 9 red cards from a standard deck.
The likelihood that our class game resulted in such a high number of red cards (or higher) if we were really drawing from a standard deck of playing cards is called a p-value. (This P-value is about 0.008)
We wanted to test the hypothesis that we were playing with a standard deck.
Data was gathered from a real-world experiment to test our hypothesis.
We asked, “How likely was it to draw the hand that we did if we drew from a standard deck?” “
We simulated 1000 games using
R, and counted the # of games that gave us at least the result we got in the experiment.
We calculated a P-value, the probability of getting results as extreme as ours (or more so!) from a standard deck.
Finally, draw a conclusion.
If we assume that our volunteer drew 10 cards from a standard deck of cards, there is about a 0.8% chance of drawing 9 red cards.
Goal: Translate data into knowledge and understanding of the world around us. Statistics is the art and science of learning from data!
The card game we played above is a perfect example of the three aspects of statistics.