1 / 40

Lecture 1 Introduction to R

Lecture 1 Introduction to R. Course structure. 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 Teetor

jabir
Download Presentation

Lecture 1 Introduction to R

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture1Introduction to R 732A44 Programming in R

  2. Course structure • 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 Teetor • knowledge of R at the advanced level: The Art of R Programming by Norman Matloff. • Lab schedule: http://www.mattiasvillani.com/teaching/programming-in-r/ 732A44 Programming in R

  3. About R • 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/ Find the mirror nearest to you when downloading 732A44 Programming in R

  5. 732A44 Programming in R

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

  7. Important menu items 732A44 Programming in R

  8. Important menu items 732A44 Programming in R

  9. Important menu items 732A44 Programming in R

  10. Other editors: Rstudio and Notepad++ Notepad++: choose ”Language” ”R” 732A44 Programming in R

  11. Getting help • Specific function • help(function) • Help browser • help.start() • Search for something in help • help.search(“expression”) • Quick reminderoffunction arguments: • args(function) • Examples ofhowtousefunction: • example(function) • If somemethod is not installed on the computer: • RSiteSearch(”expression") 732A44 Programming in R

  12. Preliminaries • 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. Working with vectors • Assign a vector 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 732A44 Programming in R

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

  15. Sequences • Use either ‘: ‘ or seq() 732A44 Programming in R

  16. Operation with vectors Important: In R, operations with vectors are performed element-by-element Some operations: • Element-wise: +-*/^ • log exp sin cossqrt • 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. Working with matrices 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

  18. Indexing • 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. Matrix operations 732A44 Programming in R

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

  21. Lists • List is a collection of objects 732A44 Programming in R

  22. Data frames 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

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

  24. Entering or editing data • Vectors, lists, data framescan be entered in R: • edit(variable) • Example: myframe<-data.frame(); edit(myframe); 732A44 Programming in R

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

  26. Read from Excel file • Save file as comma-separatedfile (csv) • Set currentworking directory: • setwd(directory) • Useread.csv2(filename) Example: setwd("Z:/732A44"); mydata<-read.csv2("Counties.csv"); 732A44 Programming in R

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

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

  29. Writing your ownfunctions • Functionwriting must alwaysend with writing the valuewhichshould be returned! • You mayalsouse ”return(value)” to show whatvalue the functionshouldreturn 732A44 Programming in R

  30. Writing your ownfunctions • Ifseveral arguments need to be returned, list may be used 732A44 Programming in R

  31. Writing your ownfunctions • Obligatory arguments and arguments by default • Variables can be specified in any order when you call the function 732A44 Programming in R

  32. Graphicalprocedures • plot(x,..) plots time series • plot(x,y) scatterplot • plot(x,y) followed by points(x,y) plotsseveralscatterplots in onecoordinate system • hist(x,..) plots a hitogram • persp(x,y,z,…) createssurfaceplots 732A44 Programming in R

  33. Debugging • 99% of all moderate-sizecodescontainmistakes in the first version • Often not easytofindmistake debug Way 1: debug a function step-by-step from the beginning: • Usedebug(function) • Useundebug(function) Example: Find a step when 1+2+4+8+… becomesequalto 500(findmistake) ! 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); 732A44 Programming in R

  34. Debugging Thingsto do in debug mode • n or <Enter> : runnextline • c: continueuntilyou exit from the loop or from the function • Any R command, for ex. findout the valueof a variable • where : prints stack trace • Q : quit the debugger 732A44 Programming in R

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

  36. Someexamples • Solving a linear system of equations 732A44 Programming in R

  37. Someexamples Regression model 732A44 Programming in R

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

  39. 732A44 Programming in R

  40. Final comments 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

More Related