210 likes | 445 Views
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. Workspace. All of the objects are the content of the R workspace
E N D
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
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?
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())
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.
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()
Saving data • write.csv() • write.table() • save() ## save a few variables • save.image() ## save the whole workspace • load() • See help for the details
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)
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
for loop • for ( VAR in VECTOR ) { STATEMENT1 STATEMENT2 ETC } • break() ## the same as in C • next() ## C uses continue()
Conditional statements • if ( CONDITION ) { STATEMENT1 STATEMENT2 ETC } • switch() if ( CONDITION ) { STATEMENT1 STATEMENT2 ETC } else{ STATEMENT3 STATEMENT4 ETC }
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 ....
Functions • FNAME <- function ( ARG1, ARG2, ETC ) { STATEMENT1 STATEMENT2 ETC return( VALUE ) }
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){}
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)
Recursive function • Example: • factorial <- function(x){ if(x==1) return( 1) else return(x*factorial(x-1)) }
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’)
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