Presentation is loading. Please wait.

Presentation is loading. Please wait.

732A44 Programming in R.  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of.

Similar presentations


Presentation on theme: "732A44 Programming in R.  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of."— Presentation transcript:

1 732A44 Programming in R

2  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of reports-.  One written final exam (computer)  Course book: R Cookbook, by Paul TeetorR Cookbook ◦ knowledge of R at the advanced level: The Art of R Programming by Norman Matloff.The Art of R Programming  Lab schedule: http://www.mattiasvillani.com/teaching/programming-in-r/ http://www.mattiasvillani.com/teaching/programming-in-r/ 732A44 Programming in R

3  R is an open source comprehensive statistical package, more and more used around the world.  R is a real programming language (compare to SAS or Minitab)  R is not low-level language as C or Visual Basic  Can be compared to Matlab by structure, by origin – free version of S + Has a lot of contributed packages covering a lot of statistical methods (also recent ones!) - Documentation is badly structured 732A44 Programming in R

4 Installing the R software R project web site: http://www.r-project.org/http://www.r-project.org/ Find the mirror nearest to you when downloading 732A44 Programming in R

5

6 Start  (All )Program  R  R 2.9.0 732A44 Programming in R

7

8

9

10 Notepad++: choose ”Language”  ”R” 732A44 Programming in R

11  Specific function ◦ help(function)  Help browser ◦ help.start()  Search for something in help ◦ help.search(“expression”)  Quick reminder of function arguments: ◦ args(function)  Examples of how to use function: ◦ example(function)  If some method is not installed on the computer: ◦ RSiteSearch(”expression") 732A44 Programming in R

12  R is case-sensitive  Separate commands by semicolon (;)  Comments: #R is a very cool language! Data assignment: Use -> or <- a<-3; 3->b; 732A44 Programming in R

13  Assign a vector 732A44 Programming in R The function c() combines individual values (comma-spaced) to a vector Printing the value on screen: Either enter the variable or use the function print() Note that the output begins with [1]. This is the row number, and in this case x is interpreted as a row vector

14 732A44 Programming in R Listing defined objects (vectors, matrices, data frames): Use the function ls() with no arguments Removing objects: Use the function rm()

15  Use either ‘: ‘ or seq() 732A44 Programming in R

16 Important: In R, operations with vectors are performed element- by-element Some operations:  Element-wise: +-*/^  log exp sin cos sqrt  length –number of elements  sum - sum of all elements  mean  max min order Logicals: TRUE or FALSE: a<-TRUE; > >= < <= != & (and) | (or) 732A44 Programming in R

17 Use the function matrix() a<-matrix( values,nrow= m,ncol= n ) values is a list of values enclosed in c(), i.e. a row vector or an already defined vector. m is the number of rows and n is the number of columns of the matrix. The number of values must be dividable by both m and n. The values are entered column-wise. The identifiers nrow= and ncol= can be omitted Note the double indexing, first number for row and second number for column 732A44 Programming in R Working with matrices

18  Positive integral indexing x[1,6] x[2:10]  Negative integral indexing x[-(1:5)] all except elements 1:5  Indexing entire row or column x[2,] entire row 2  Finding elements satisfying specific condition: ◦ x[x>mean(x)] 732A44 Programming in R

19

20  Matrix operators/functions:  transpose b=t(a) b = a T  Inverse b=solve(a) b = a -1 (when needed) 732A44 Programming in R

21  List is a collection of objects 732A44 Programming in R

22 Collecting vectors and matrices with the same number of rows in a data frame Use the function data.frame( object 1, object 2, …, object k ) Matrices need to be protected, otherwise each column of a matrix will be identified as a single object in the data frame. Protection is made with the function I() 732A44 Programming in R Data frames

23 Objects within a data frame can be called upon using the syntax dataframe $ object 732A44 Programming in R Data frames

24  Vectors, lists, data frames can be entered in R: ◦ edit(variable)  Example: myframe<-data.frame(); edit(myframe); 732A44 Programming in R

25 Names of objects within a data frame can be called, set or changed by handling the object names() 732A44 Programming in R Data frames

26 1. Save file as comma-separated file (csv) 2. Set current working directory: ◦ setwd(directory) 3. Use read.csv2(filename) Example: setwd("Z:/732A44"); mydata<-read.csv2("Counties.csv"); 732A44 Programming in R

27 if (expr) { … } else{ … } If you need to connect several conditions, use ’&’, ’&&’, ’| ’ or ’||’ 732A44 Programming in R

28 for (name in expr1 ) { … } while (condition) { … } 732A44 Programming in R

29  Function writing must always end with writing the value which should be returned!  You may also use ”return(value)” to show what value the function should return 732A44 Programming in R

30  If several arguments need to be returned, list may be used 732A44 Programming in R

31  Obligatory arguments and arguments by default  Variables can be specified in any order when you call the function 732A44 Programming in R

32  plot(x,..) plots time series  plot(x,y) scatter plot  plot(x,y) followed by points(x,y) plots several scatterplots in one coordinate system  hist(x,..) plots a hitogram  persp(x,y,z,…) creates surface plots 732A44 Programming in R

33  99% of all moderate-size codes contain mistakes in the first version  Often not easy to find mistake  debug Way 1: debug a function step-by-step from the beginning:  Use debug(function)  Use undebug(function) Example: Find a step when 1+2+4+8+… becomes equal to 500 (find mistake) ! 732A44 Programming in R myfun<-function(x) { i<-1; r<-0; while (r!=x) { r<-r+i; i<-i*2; } return(i); } debug(myfun); a<-myfun(500); #after some steps undebug(myfun);

34 Things to do in debug mode  n or : run next line  c : continue until you exit from the loop or from the function  Any R command, for ex. find out the value of a variable  where : prints stack trace  Q : quit the debugger 732A44 Programming in R

35 Way 2: insert browser() in the suspicious place in the code: 732A44 Programming in R myfun<-function(x) { i<-1; r<-0; while (r!=x) { r<-r+i; if(r>500) browser(); i<-i*2; } return(i); }

36  Solving a linear system of equations 732A44 Programming in R

37 Regression model

38 732A44 Programming in R Use help(”lm”) to learn more about this function

39 732A44 Programming in R

40 Huge more to find out! Read course books Use PDF manuals Use the help function help(” function ” ) or ? function Use Google (search for “R: what you are looking for”) 732A44 Programming in R


Download ppt "732A44 Programming in R.  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of."

Similar presentations


Ads by Google