Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia."— Presentation transcript:

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

2 Chap 2. Programming with R Workspace Working directory Input and output Flow control Functions Packages Writing R scripts

3 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?

4 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())

5 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.

6 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(Airpasse nengers) ls()

7 Saving data write.csv() write.table() save() ## save a few variables save.image() ## save the whole workspace load() See help for the details

8 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)

9 Flow Control while loop while ( 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

10 for loop for ( VAR in VECTOR ) { STATEMENT1 STATEMENT2 ETC } break() ## the same as in C next() ## C uses continue()

11 Conditional statements if ( CONDITION ) { STATEMENT1 STATEMENT2 ETC } switch() if ( CONDITION ) { STATEMENT1 STATEMENT2 ETC } else { STATEMENT3 STATEMENT4 ETC }

12 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....

13 Functions FNAME <- function ( ARG1, ARG2, ETC ) { STATEMENT1 STATEMENT2 ETC return( VALUE ) }

14 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){}

15 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)

16 Recursive function Example: factorial <- function(x){ if(x==1) return( 1) else return(x*factorial(x-1)) }

17 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’)

18 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 mwfywx@gmail.com Hint: ? as.Date() Help: http://www.statmethods.net/input/dates.html


Download ppt "Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia."

Similar presentations


Ads by Google