Lab1: Introduction to R


Getting Started with R

Click on the R icon

If no R icon, go to Start and then All Programs to find R

To quit R:

> q()

What does it ask you? What does this mean?

For R HELP, for example to get help about the function runif ,

> help(runif)

For R HELP in html format

If you go to Help on the R toolbar and select R language (html), you will get the html version of help.


Example 1: Let the games begin.

Generate a sample of 100 normal random variables, N(0,1).

> help(rnorm)

> rnorm(100) What happens with the numbers?

It would be better to:

> temp <- rnorm(100)

What is in the object temp?

What is the length of the oject temp?

Make a informative plot of temp?

If this were part of a homework problem, you might want to copy and paste the R code and plot into a Word Document?
Try it!

Note: To copy and paste from the graphics window,
Right click on the mouse and select Copy as metafile, then Paste.


Example 2: Cards and Poker

We will need 3 functions to play poker. I have written these functions, so the help files are not in R, yet.

They are not too complicated. The 3 functions are:

cards.r
rperm.r
poker.r

There are 2 ways to get these functions for your very own.

  1. Copy and paste the code into the R command window for each function.
  2. Use the R source command which sources the code in the files. You will need 3 source commands:

    > source("http://www.rohan.sdsu.edu/~babailey/stat550/cards.r")
    > source("http://www.rohan.sdsu.edu/~babailey/stat550/rperm.r")
    > source("http://www.rohan.sdsu.edu/~babailey/stat550/poker.r")

For practice, generate 1 hand of 5 card poker.

IMPORTANT If you want to reproduce your results, you will have to set the seed of the random number generator.
There is an R function set.seed

To use it,

> set.seed(1)
> poker(draw=5, hand=1)

How can you draw the same hand again?


Intro to R Part I: For more practice with R

Here is first introduction to R: ISU

At the bottom of the page is how to get the "An Introduction to R"


Intro to R Part II:

If you want even more practice:
Try going through the following Intro. Lessons. The # sign is a comment, so you can copy and paste.

  • R vectors, lists and functions
  • R arithmetic and logical operators
  • R simple functions
  • R univariate random variable generators