R-Programming

What is R?

R is an open-source (GPL) statistical environment modeled.

The benefits of R for an introductory student are :

  • R is free. R is open-source and runs on UNIX, Windows and Macintosh.

  • R has an excellent built-in help system.

  • R has excellent graphing capabilities.

  • Students can easily migrate to the commercially supported S-Plus program if commercial software is desired.

  • R’s language has a powerful, easy to learn syntax with many built-in statistical functions.

  • The language is easy to extend with user-written functions.

  • R is a computer programming language. For programmers it will feel more familiar than others and for new computer users, the next leap to programming will not be so large.

Starting R

R version 3.5.1 (2018-07-02) -- "Feather Spray"

Copyright (C) 2018 The R Foundation for Statistical Computing

Platform: x86_64-apple-darwin15.6.0 (64-bit)


R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.


Natural language support but running in an English locale


R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.


Type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an HTML browser interface to help.

Type 'q()' to quit R.


>

Entering data with c

The most useful R command for quickly entering in small data sets is the c function. This function combines, or concatenates terms together.

As an example, suppose we have the following count of the number of t as per page of these notes: 2 3 0 3 1 0 0 1


> t = c(2,3,0,3,1,0,0,1)

> t

[1] 2 3 0 3 1 0 0 1

Applying a function

> mean(t)

[1] 1.25

> median(t)

[1] 1

> var(t)

[1] 1.642857

Data is a vector

The data is stored in R as a vector. This means simply that it keeps track of the order that the data is entered in. In particular there is a first element, a second element up to a last element. This is a good thing for several reasons:

  • Vectors are a mathematical object. There are natural extensions of mathematical concepts such as addition and multiplication that make it easy to work with data when they are vectors.

> t.draft1 = c(2,3,0,3,1,0,0,1)

> t.draft2 = c(0,3,0,3,1,0,0,1)

> t.draft1

[1] 2 3 0 3 1 0 0 1

> t.draft2

[1] 0 3 0 3 1 0 0 1

> t.draft2=t.draft1

> t.draft2

[1] 2 3 0 3 1 0 0 1

> t.draft2[1]=0

> t.draft2

[1] 0 3 0 3 1 0 0 1