statistical software an introduction to statistics using r instructed by jinzhu jia

18
Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Upload: sydney-cummings

Post on 23-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Statistical SoftwareAn introduction to Statistics Using R

Instructed by Jinzhu Jia

Page 2: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Chap 2. Programming with R

Workspace

Working directory

Input and output

Flow control

Functions

Packages

Writing R scripts

Page 3: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Workspace

All of the objects are the content of the R workspace

What objects do you have in your R workspace ? objects() ls() ls.str() library(lsr) ## Note: the first time we see using a

package who() ## Anytime you want to use who(), please use

the command ``library(lsr)” first. Will learn more later

Differences?

Page 4: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Removing variables

rm(X,Y) ## variabes X and Y will be removed from the workspace

rm(list = c(‘X’,’Y’)) ## the same as the above command

rm(list = ls())

Page 5: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Working directory

Working directory specifies what folder your data will be read from or save to.

getwd() ## wd denotes working directory

setwd(‘./newfolder’) ## ./ denotes the current folder

You can also use Misc->Changing working directory to choose your folder in your MAC

list.files() ## list all the files in the current wd.

Page 6: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Reading data

read.csv() ## most frequently used

read.table() ## frequently used

scan() ##

readLines() ##

data() ## R has a few data sets in its packages

## this function lists dataset names stored in the workspace, such as

data(‘data set name’) ## loads the data

Try: data()

data(Airpassenengers)

ls()

Page 7: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Saving data

write.csv()

write.table()

save() ## save a few variables

save.image() ## save the whole workspace

load()

See help for the details

Page 8: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

R script

It is a text file

You can use it to write all your R commands

Write your own package

File->New Scrip ## you will get an empty script file

source(rscipt_File)

Page 9: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Flow Control

while loopwhile ( CONDITION )

{ STATEMENT1

STATEMENT2 ETC

}

Note: { } is a block of statements; if you do not use {}, only the first statement will be executed in the loop

Page 10: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

for loop

for ( VAR in VECTOR ) { STATEMENT1

STATEMENT2

ETC }

break() ## the same as in C

next() ## C uses continue()

Page 11: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Conditional statements

if ( CONDITION )

{

STATEMENT1

STATEMENT2

ETC

}

switch()

if ( CONDITION ) { STATEMENT1 STATEMENT2 ETC }else { STATEMENT3 STATEMENT4 ETC }

Page 12: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Rule of switch()

switch(ExpR,….)

Expr: an expression evaluating to a number or a character string

If the value of EXPR is not a character string it is coerced to integer. The corresponding element of …. will be evaluated

If EXPR evaluates to a character string then that string is matched (exactly)to the names of the elements in ....

Page 13: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Functions

FNAME <- function ( ARG1, ARG2, ETC )

{

STATEMENT1

STATEMENT2

ETC

return( VALUE )

}

Page 14: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Binary operator

%*% ## matrix multiplication

%% ## modulus 5%%2 = 1

%/% ## integer division 5%/%2 = 1

%in% ## ‘a’ %in% c(‘a’,’b’,’c’) = T

Create your own binary operator

‘%+%’ <- function(s1,s2){}

Page 15: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Default parameters

? Seq

seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL, ...)

We see a few parameters in function seq(), each of wich has default parameters.

Example: mysub = function(x,y=1){z = x-y}mysub(2) = mysub(2,1) = mysub(x=2)

Page 16: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Recursive function

Example:

factorial <- function(x){ if(x==1) return( 1) else return(x*factorial(x-1)) }

Page 17: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Packages

A package is a collection of objects

It usually completes one special task

Use a package: libray(‘package name’)

Install a package: install.packages(‘package name’)

Page 18: Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Homework

Find all of the pairs of twin primes less than 10^6 using R and report the total number.

Print a calendar for any given month and year as input.

For example: mycal(2014,3) will give you a table like this:

Send your code and results to [email protected]

Hint: ? as.Date()

Help: http://www.statmethods.net/input/dates.html