18
Statistics package Graphics package Programming language Can be used to share/reproduce analyses Many new packages being created - can be downloaded and easily installed Largely text-based interface Introduction to the R language Introduction to the R language What is R? What is R?

Statistics package Graphics package Programming language Can be used to share/reproduce analyses

  • Upload
    holden

  • View
    44

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction to the R language. What is R?. Statistics package Graphics package Programming language Can be used to share/reproduce analyses Many new packages being created - can be downloaded and easily installed Largely text-based interface. Strengths of R. - PowerPoint PPT Presentation

Citation preview

Page 1: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Statistics package

Graphics package

Programming language

Can be used to share/reproduce analyses

Many new packages being created - can be

downloaded and easily installed

Largely text-based interface

Introduction to the R languageIntroduction to the R language

What is R?What is R?

Page 2: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Statistical and numerical methods

High quality visualization and graphics tools

Effective, extensible user interface

Compiles and runs on UNIX, Windows and Macs

Open source i.e., freely software

Strengths of RStrengths of R

Page 3: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Software, documentation, manuals and support:

More information about R

http:// www.r-project.org/

The Comprehensive R Archive Network

http://cran.r-project.org/

The 'official' introduction to R

http://cran.r-project.org/doc/manuals/R-intro.pdf

R software R software

Page 4: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

R has several packages available

R has more than 1000 functions and commands

It is impossible to remember them all

Each function may also take several arguments

One needs to use the help system and manuals

regularly

Working with RWorking with R

Page 5: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Once R is installed on your system, click on the R icon

Upon opening, you will see the following console window:

Getting startedGetting started

Page 6: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

The R program issues a prompt ‘>’ when it expects

input commands

E.g., type in 2+2 and press the enter key

You will see:

Prompt and Command linePrompt and Command line

> 2+2

[1] 4

>

The result is 4.

The [1] says “first requested element will follow”.

Here, there is just one element.

The prompt returns waiting for further input.

The result is 4.

The [1] says “first requested element will follow”.

Here, there is just one element.

The prompt returns waiting for further input.

Page 7: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

For each “project” on which one works one should use a

different directory (=folder)

At the end of each session, R asks whether one wants

to save the workspace image - all objects are saved

On double clicking on a workspace image file, R starts

and loads the workspace - all objects are restored

When the workspace image is saved, the history of

recently typed commands is saved

Some peculiarities of RSome peculiarities of R

Page 8: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

The workspace stores all commands

Previous commands can be retrieved using the up/down

arrows

R is case sensitive

Using UNIX platforms, UNIX commands can be used:

Elementary RElementary R

mkdir test -> make directory called test

cd test -> change to test directory

mkdir test -> make directory called test

cd test -> change to test directory

Using windows, use the File menu to open and save

files, or change directories

Page 9: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Excel files should first be saved as tab-delimited text

files, before they can be read into R

In R, the full pathname of files should be specified – go

to the File menu, change directories to specify the

location of the file to be opened

Open files with filenames and their extentions

See R Data Import/Export manuals in the Help menu

Importing DataImporting Data

Page 10: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

So, to view a tab-delimited file, stored on your desktop:

1. Set directory in the File menu

2. At the prompt, type: read.delim(“filename.txt")

3. The data will now be shown on the R console

4. Naming the datafile:

x=read.delim(“filename.txt")

Importing Data - exampleImporting Data - example

Various ways of reading in the files

> x = read.delim(“filename.txt”)

> x <- read.table (“filename.txt”)

> x <- read.csv (“filename.txt”)

Page 11: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

The exit or quit command is > q()

OR click on the File menu and then on Exit

OR click on the X in the top right hand corner of the R

window

Remember, there will be a message asking whether to

save the workspace image. Clicking Yes (the safe

option) will save all the objects that remain in the

workspace – any that were there at the start of the

session and any that have been added since.

Saving and QuittingSaving and Quitting

Page 12: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

You may have the same variable names within two

different datasets, and so it’s necessary to attach a

dataset: attach(filename)

When you ask R to use a value of a variable it needs to

find, R searches through several ``environments'' for these

variables

By attaching a data frame, you put the names into the

second environment searched (the name of the dataframe

is in the first). These are masked by any variables which

already have the same name

Attaching filesAttaching files

Page 13: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Important things to remember:

You can change the values of the variables in the data

frame, but you must remember to reference the data frame

Variables removed from unattached files, will not be visible

when listing variables (where ls() lists all known variables),

but those variables in fact still there

You must detach the dataset between uses of these

variables, or you may forget which variable you are

referring to: detach(filename)

Attaching and detaching filesAttaching and detaching files

Page 14: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Getting HelpGetting Help

> help ( ) provides help on how to use ‘help’

> help (topic) provides help on a specific topic

> help.start ( ) opens R documentation on the internet

> help ( ) provides help on how to use ‘help’

> help (topic) provides help on a specific topic

> help.start ( ) opens R documentation on the internet

Various options are available on the help function

Also, use the help menu

This is incredibly useful – get to know it and use it!

Page 15: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

R as a calculatorR as a calculator

R will evaluate basic calculations which are typed into

the console (input window)

Try a few examples ….

Page 16: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Value AssignmentsValue Assignments

Values can be assigned using the <- operator, which

consists of the two characters ‘<’ (“less than”) and ‘-’

(“minus”) occurring side-by-side, and it ‘points’ to the

object receiving the value of the expression.

Example:> x <- 10

> y <- 20

> x + y

[1] 30

>

variables

Page 17: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Value AssignmentsValue Assignments

More complicated calculations

To obtain the number (or other value) stored in any

letter:

- type the letter followed by enter

- type print (letter)

- type show (letter)

Page 18: Statistics package Graphics package Programming language Can be used to share/reproduce analyses

Simple operationsSimple operations

Try some of these simple operations:

Add: 10 + 20

Multiply: 10 * 20

Divide: 10/20

Raise to a power: 10 ** 20