introduction to gtech 201 session 13. what is r? statistics package a gnu project based on the s...

22
Introduction to GTECH 201 Session 13

Post on 18-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Introduction to

GTECH 201Session 13

Page 2: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

What is R?

Statistics package

A GNU project based on the S language

Statistical environment

Graphics package

Programming language

Page 3: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Getting Started Starting R

Page 4: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Getting Help

Getting help > help ( ) provides help on how to use

‘help’> help (topic) provides help on a specific

topic> help.start ( ) brings you to a web interface

to the R documentation

Page 5: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

R functions take arguments (information that you put into the function which goes between the brackets) and can perform a range of tasks. In the case of the ‘help’ function the task is to display information from the R documentation files.

R Functions

help ( ) is an R function

Page 6: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

R as Calculator

R will evaluate basic calculations which you type into the console (input window)

Page 7: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Assigning Values With the <- operator With a regular = equal sign

Page 8: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

R as Calculator

In the previous example x and y are variables. We obtained the sum of x and y by typing x + y

In the same way we could carry out much more complicated calculations

Generally you can obtain the number (or other value) stored in any letter by typing the letter followed by enter (or by typing print (letter) or show (letter))

Page 9: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Simple Operations

Add 10 + 20 Multiply 10 * 20 Divide 10 / 20 Raise to a power 10 ** 20 Modulo 10 %/% 20 Integer division 10 %% 4

Page 10: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

In R you can think of vectors as being equivalent to a single column of numbers.

You can create a vector using the c( ) function as follows: x <- c( )

e.g. x <- c(1,2,4,8) creates a column of the numbers 1,2,4,8

Vectors

Page 11: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

When you carry out simple operations (+ - * /) on vectors in R that have the same number of entries R just performs the normal operations on the numbers in the vector entry by entry

If the vectors don’t have the same number of entries then R will cycle through the vector with the smaller number of entries

Vectors can be assigned by putting together other vectors

Simple Operations on Vectors

Page 12: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming
Page 13: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming
Page 14: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming
Page 15: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Combining Vectors

Page 16: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Matrices and Lists

Matrix Rectangular table of data of the same type Arrays are 3-, 4-, .. n-dimensional matrices

List An ordered collection of data of arbitrary

types > doe = list(name="john",age=28,married=F)

Page 17: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Data Frames

The tables we know from Excel Each column has the same type But different columns may be of

different type

Page 18: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Subsetting

Individual elements of a vector, matrix, array or data frame are accessed with “[ ]” by specifying their index, or their name

Page 19: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Storing Data

Every R object can be stored into and restored from a file with the commands “save” and “load”

> save(x, file=“x.Rdata”)

> load(“x.Rdata”)

Page 20: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

R Import and Export

Most programs (e.g. Excel) know how to deal with rectangular tables in the form of tab-delimited text files

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

also: read.table, read.csv

> write.table(x, file=“x.txt”, sep=“\t”)

Page 21: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Importing Data Caveats

Type conversions The read functions try to guess and

autoconvert the data types of the different columns (e.g. number, factor, character)

Special characters Delimiter character (space, comma,

tabulator) cannot be part of a data field To circumvent this, text may be “quoted”

Page 22: Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming

Getting Help (Again) Html search

engine