====== Introduction to R ====== ===== Why R ? ===== {{pub:pics:dsc2001.jpg?300}} Ross Ihaka and Robert Gentleman at DSC 2001 R is a programming language and environment for data analysis developed in nineties by Ross Ihaka and Robert Gentleman from University of Auckland, New Zealand. It is an open-code implementation of language S developed in 1976 by John Chambers and his collaborators at Bell Laboratories. A commercial version of S is known as S-Plus. In the new millennium many statisticians joined the project that became the main environment for development and publication of new statistical (and other) methods. In June 18, 2017 there were 10852 packages on the [[http://cran.at.r-project.org/|CRAN]]. See also [[https://r-forge.r-project.org/|R-Forge]], [[https://github.com/trending/r|GitHub]]/[[http://r-pkgs.had.co.nz/git.html|Wickham]]. * R is free - we can install it and use freely on our home computer. It runs on Windows, Linux/Unix and Mac. * R is open-code - we can look inside to learn, and also change to adapt for our needs. * R is interpreted - easy to use; check execution of procedures step by step. * R has a large community of users. * R supports high quality data visualizations on different devices. * R was developed as a programming language/environment for statistics; but found a broader usage - decision support, biochemistry, finance etc. The main rival to R is [[https://www.python.org/|Python]] with [[http://pandas.pydata.org/|Pandas]], and in the future [[https://julialang.org/|Julia]]. ===== Some links ===== * Project R: http://www.r-project.org/ * CRAN (The Comprehensive R Archive Network): http://cran.at.r-project.org/ * web journal R-news: http://cran.r-project.org/doc/Rnews/ ; https://journal.r-project.org/ * data sources: * DASL (The Data and Story Library) http://dasl.datadesk.com/, * The world factbook https://www.cia.gov/library/publications/the-world-factbook/ * The UCI Knowledge Discovery in Databases Archive http://kdd.ics.uci.edu/ * conference UseR!: http://www.r-project.org/conferences.html ===== R interpreter ===== Executing program R the R Console window appears with basic info about R version. It ends with a character >, that informs us that R is ready to receive commands (expressions). We terminate the session using q(), or Z, or selecting File/Exit, or with a click on X button of the program window. Examples of simple expressions are mathematical expressions - R can be used as a calculator: > 1+1 [1] 2 > 3^2 + 4^2 [1] 25 > 3 + 4 * 5 [1] 23 > (3 + 4)*5 [1] 35 > pi [1] 3.141593 > pi * 2.5^2 [1] 19.63495 > 4*atan(1) [1] 3.141593 > (1+sqrt(5))/2 [1] 1.618034 For a list and details about available functions see help(Arithmetic), help(Trig), help(complex), help(log), help(Round), help(sqrt), etc. To store a computed value we use an assignment statement: > a <- 14 > a [1] 14 > (7 -> a) [1] 7 > a [1] 7 > (a <- a+1) [1] 8 > b <- c <- a > b [1] 8 > c [1] 8 > d Error: object "d" not found > rm(b) > b Error: object "b" not found > (b <- a * (d <- 3)) [1] 24 > b [1] 24 > d [1] 3 > B Error: object "B" not found > (B <-2009) [1] 2009 The basic data types in R are: numeric, integer, complex, logical, character, and raw. They can be combined into structured objects using: vector, matrix, array, list, etc. For testing and converting types we have functions * is.//type//(x) - is the object x of type //type// ? * as.//type//(x) - try to convert object x into object of type //type//. * typeof(x) - returns the type of x; similar class(x) and mode(x). R is a programming language. About the basic control statements see {{pub:pdf:rcontrol.pdf|R-control}}. \\ \\ [[ru:7iss#labs|Back to 7ISS Labs]]