Introduction to R

Why R ?

dsc2001.jpg 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 CRAN. See also R-Forge, GitHub/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 Python with Pandas, and in the future Julia.

Some links

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 <Ctrl>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 R-control.



Back to 7ISS Labs

ru/7iss/labs/r1.txt · Last modified: 2017/06/18 21:34 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki