short introduction to r
play

Short Introduction to R Paulino Prez 1 Jos Crossa 2 1 ColPos-Mxico 2 - PowerPoint PPT Presentation

Short Introduction to R Paulino Prez 1 Jos Crossa 2 1 ColPos-Mxico 2 CIMMyT-Mxico September, 2014. SLU,Sweden Short Introduction to R 1/51 Contents Introduction 1 2 Simple objects 3 User defined functions 4 Graphs 5 Importing


  1. Short Introduction to R Paulino Pérez 1 José Crossa 2 1 ColPos-México 2 CIMMyT-México September, 2014. SLU,Sweden Short Introduction to R 1/51

  2. Contents Introduction 1 2 Simple objects 3 User defined functions 4 Graphs 5 Importing data Installing packages 6 7 Questions SLU,Sweden Short Introduction to R 2/51

  3. Introduction ¿R? Contents Introduction 1 ¿R? R and other sofwares Manuals and help Sample R session Simple objects 2 Vectors Matrices data.frame User defined functions 3 Graphs 4 Plotting user defined functions Plot function More functions for graphs Importing data 5 Installing packages 6 Questions 7 SLU,Sweden Short Introduction to R 3/51

  4. Introduction ¿R? ¿R? R is a software for statistical analysis and graphics. It was developed by Ross Ihaka y Robert Gentleman. R was specially designed to perform data analysis, but can be used also as a programming language. R is distributed freely under the GNU license(General Public Licence). The development is done by the R Development Team. R is available as source code and binary files compiled for windows and Mac. The R language allows the user to use loops to perform complex analysis. SLU,Sweden Short Introduction to R 4/51

  5. Introduction R and other sofwares Contents Introduction 1 ¿R? R and other sofwares Manuals and help Sample R session Simple objects 2 Vectors Matrices data.frame User defined functions 3 Graphs 4 Plotting user defined functions Plot function More functions for graphs Importing data 5 Installing packages 6 Questions 7 SLU,Sweden Short Introduction to R 5/51

  6. Introduction R and other sofwares R and other softwares Why R ? R is a free software, it can be executed in Windows, Linux and Mac. Excellent documentation and graphical capabilities. Most of the programs written in S plus can be run in R. It is powerful and easy to learn. It can be extended through the use of packages. SLU,Sweden Short Introduction to R 6/51

  7. Introduction R and other sofwares Disadvantages Graphical user interface not as good as in other softwares. Lack of commercial support (partially true). SLU,Sweden Short Introduction to R 7/51

  8. Introduction R and other sofwares Installation in a Windows environment Go to http://www.r-project.org and download the windows binary, Figure 1: R web site. SLU,Sweden Short Introduction to R 8/51

  9. Introduction R and other sofwares Continue... Go to the Downloads section and select CRAN, Figure 2: CRAN mirrors SLU,Sweden Short Introduction to R 9/51

  10. Introduction R and other sofwares Continue... Select the software for you OS, Figure 3: Executables for various platforms. SLU,Sweden Short Introduction to R 10/51

  11. Introduction R and other sofwares Continue... Download the base software, Figure 4: Download R-base. SLU,Sweden Short Introduction to R 11/51

  12. Introduction R and other sofwares Continue... Double click in the installer, Figure 5: Installing R. SLU,Sweden Short Introduction to R 12/51

  13. Introduction Manuals and help Contents Introduction 1 ¿R? R and other sofwares Manuals and help Sample R session Simple objects 2 Vectors Matrices data.frame User defined functions 3 Graphs 4 Plotting user defined functions Plot function More functions for graphs Importing data 5 Installing packages 6 Questions 7 SLU,Sweden Short Introduction to R 13/51

  14. Introduction Manuals and help Manuals Once that you install R, you will have access to the following manuals in PDF format: An Introduction to R R Reference Manual R Data Import/Export R Language Definition Writing R Extensions R Internals R Installation and Administration SLU,Sweden Short Introduction to R 14/51

  15. Introduction Manuals and help Continue... Furthermore: Contributed Docs ( http://cran.r-project.org/other-docs.html ). R-help mailing list archives ( http://cran.r-project.org/search.html ). Mailing list. Reference card. Summary of most useful R commands ( http://www.rpad.org/Rpad/Rpad-refcard.pdf ) S Programming, W. Venables and B. Ripley. See http://www.stats.ox.ac.uk/pub/MASS3/Sprog . SLU,Sweden Short Introduction to R 15/51

  16. Introduction Sample R session Contents Introduction 1 ¿R? R and other sofwares Manuals and help Sample R session Simple objects 2 Vectors Matrices data.frame User defined functions 3 Graphs 4 Plotting user defined functions Plot function More functions for graphs Importing data 5 Installing packages 6 Questions 7 SLU,Sweden Short Introduction to R 16/51

  17. Introduction Sample R session Sample R session Go to a Start->Programs->R->R-3.x.y , the working environment is as that shown in the next Figure. Figure 6: R ready to process commands. SLU,Sweden Short Introduction to R 17/51

  18. Introduction Sample R session The symbol > is the command prompt. We can write command there, for example to show some help about matrices, ?matrix then Enter The help system can be accessed through the command line using the following functions: ?text help.start() help.search("text to search") apropos("search for some thing similar to...") SLU,Sweden Short Introduction to R 18/51

  19. Introduction Sample R session Code editors A set of R commands is usually known as “script". There are several text editors. The one included by default in Windows installations is not as fancy as others that have syntax highlighting, for example Tinn-R, or R-studio. The standard text editor in R can be accessed from the File menu, File->New Script Figure 7: Code editor in R SLU,Sweden Short Introduction to R 19/51

  20. Introduction Sample R session Example (performing basic calculations): 7+4 2*3*(1+2) The commands are written in the text editor, then the commands are selected and the pop-up menu is activated, one of the entries in the menu has an option to execute the code. The result will appear in the R console. Figure 8: Executing R commands from the text editor. SLU,Sweden Short Introduction to R 20/51

  21. Introduction Sample R session Commands written in the text editor can be saved and restored for editing later. There exists a lot of text editors for writing R scripts, for example: WinEdit (shareware) SciViews (freeware) Tinn-R (freeware) Emacs (free) Rstudio (free) SLU,Sweden Short Introduction to R 21/51

  22. Introduction Sample R session Continue... Figure 9: Rstudio. SLU,Sweden Short Introduction to R 22/51

  23. Simple objects R works manipulating “objects” . The objects are manipulated using functions and operators. The most basic objects are: vectors (type numeric or character) Matrix data.frame Lists Functions Some useful functions... General pourpouse: sqrt(),log(),exp(),sin(),cos() , etc. Related to statistics: mean(), sd(), var(), quantile() , etc. The assignment operator is = with R>=1.4.0 or <- in any R version. SLU,Sweden Short Introduction to R 23/51

  24. Simple objects Notes: R distinguish between upper and lowercase letters The symbol "#" is used to comment the code Object’s names can contain any combination of characters, except spaces and special symbols, for example "$","%","#" , etc. Missing data can be represented with the special symbol "NA" (Not Available), and errors in computations for example dividing by 0 with the special symbol "NaN" (Not a Number) or "Inf" SLU,Sweden Short Introduction to R 24/51

  25. Simple objects Vectors Contents Introduction 1 ¿R? R and other sofwares Manuals and help Sample R session Simple objects 2 Vectors Matrices data.frame User defined functions 3 Graphs 4 Plotting user defined functions Plot function More functions for graphs Importing data 5 Installing packages 6 Questions 7 SLU,Sweden Short Introduction to R 25/51

  26. Simple objects Vectors Vectors Vectors are created using the functions c(),seq(),:, rep() . Examples: a=c(1,2,3,4,5) a b=c("a","b","c") b d=1:10 d e=seq(1,10,by=0.5) e f=seq(1,10,length.out=20) f g=rep(10,3) g h=c(e,f) h SLU,Sweden Short Introduction to R 26/51

  27. Simple objects Vectors Vector operations We can perform most common operations using vectors with the same length. Operations are performed element wise. Examples: a=c(1,2,3) b=c(2,3,5) a+b #sum a and b a-b #a-b a*b #element wise product a^b #power function 3*a+2*b #product and sum a/b #element wise quotient a^2 #takes the square of each element It is also possible to apply a function to a vector, for example: exp(a) #Exponential function log(a) #logarithm function d=sqrt(a)+log(b) #square root and logarithm d #shows d SLU,Sweden Short Introduction to R 27/51

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend