R Programming
S i m p l y I n D e p t h
B y A j i t S i n g h
R Programming : Simply In Depth
Contents
Introduction
Working with d a t a
Plotting with R
Programming with R
RStudio
Hints and tips in R
Bibliography Preface
This book will teach you how to program in R. Youll go from loading data to writing your own functions (which will outperform the functions of other R users).This is an understandable approach to learning R. Visualizing and modeling data are complicated skills that require a programmer's full attention. It takes expertise, judgement, and focus to extract reliable insights from a data set.
Introduction
Introduction
W h a t is R?
How t o obtain R?
Help and documentation in R
Default system and R settings
Working directory. Saving the session and variables
Basic concepts: objects, workspace andworking directory Scripts
Packages manipulation
External editors
The R Project for Statistical Computing ( http://www.r-project.org/)
What is R?
T h e R Proj ect for Statistical Computing ( http://www.r-project.org/ ). I R is a language and environment for statistical computing and graphics.
I It is similar to the S language (Bell Laboratories, 1970). Th e R project was initiated by Robert Gentleman and Ross Ihaka (University of Auckland, New Zealand, in the early 1990s) and has been developed with contributions from all over the world since mid-1997.
I R provides a wide variety of statistical and graphical techniques, and is highly extensible (active community of developers).
I R is available as Free Software.
How t o obt a i n R?
Installation
I T h e R system consists on two major parts:
I The base system ( w h a t you need to install R for the first time).
I The collection of contributed packages.
I Sources, binaries and documentation for R can be obtained via CRAN (Comprehensive R Archive Network).
I Choose a location in http://cran.r-project.org/mirrors.html .
I For m os t users (Windows, Mac OS X and some Linux distributions) is sufficient to download and install precompiled binaries for base distribution (following the instructions by the installer).
I Run the setup program (.exe in Windows, .app in Mac).
I By default, R is installed into %ProgramFiles%R.
R Core Team (2012). More bibliography R Installation and Administration. R Foundation for Statistical Computing. Vienna, Austria. ISBN 3-900051-09-7
http://cran.r-project.org/doc/manuals/R-admin.html
Help a n d d ocu me n t a t i on in R
Documentation in R
Different forms of documentation for the R system:
I Electronic manuals.
http://cran.r-project.org/manuals.html
I A wide collection of books and other publications related t o R. http://www.r-project.org/doc/bib/R-publications.html http://www.r-project.org/other-docs.html
I R FAQ.
http://cran.r-project.org/doc/FAQ/R-FAQ.html
I Online help (with the base distribution and packages).
My first session in R
I To run R, click on the R icon (or go t o Programs I R).
I Inside the R GUI window, there is a menu bar, a toolbar, and the R console.
( 1 )
Figure: Rs graphical user interface forWindows
The R console
I T h e R console is where you type commands and R system responds. I This window displays basic information about R and a command prompt > . I T h e prompt > indicates t h a t R is waiting for you t o type commands.
I Sometimes the symbol + appears a t the left-hand side of the screen instead of > . This means t h a t t h e last command you typed is incomplete.
( 2 )
Figure: R as a calculator. The [1] indicates that this is the first (and in this case only) result from the command
The R console
I At its m os t basic level, R can be viewed as a calculator. T h e elementary arithmetic operators are +, -, *, / and ^ for raising t o a power.
> 7 * (3 + 2)/2
[1] 17.5
> 2^3
[1] 8
I Most of the work in R is done through functions. For example, if we want to compute 9 in R we type:
> sqrt (9)
[1] 3
This tells R t o call the function named sqrt . T h e parentheses surround the argument list.
I T h e natural logarithm can be computed with the function log . > log (5)
[1] 1.609
Numeric functions
R function Description
sqrt (x) square root of x exp (x) exponential function e x log (x) natural logarithm ofx
log10 (x) common logarithm ofx abs (x) absolute value ofx sin (x) sine of x
cos (x) cosine ofx
tan (x) tangent ofx
Online help
I Once R is installed, there is a comprehensive built-in help system. I For general help type:
> help.start ()
I For help about a function: > help (log) # Equivalent to ?log
I You can also list all functions containing a given string.
( 3 )
> apropos ( "log" )
I To show an example of a function, type: > example (log)
First objects
I R lets you assign values t o variables and refer t o t h e m by name. I In R, we use the symbol < for assigning values t o variables. We can see the results of the assignment by typing the name of our new object. > x <- 3
> x
[1] 3
I T h e equals sign = can also be used as assignment operator in m os t circumstances. > y = 5
> y
[1] 5
I R is case sensitive soX is n ot the same as x.
> X
Error: object X not found
I Variable names should not begin with numbers or symbols and should n o t contain blank spaces.
First objects
I Character strings in R are made with matching quotes:
> myname < "Bea"
> myname
[1] "Bea"
I TRUE and FALSE are reserved words denoting logical constants in the R language. > mylog <- TRUE
> mylog
[1] TRUE
Special values used in R
I T h e NA values represent missing values ( n ot available).
I Positive and negative infinity are represented withInf and -Inf, repectively:
> 1/0
[1] Inf
> -5/0
[1] -Inf
I In R,NaN stands for "N o t a Number".
( 4 )
> sqrt (-4)
Warning: NaNs produced [1] NaN
Workspace
I We have created several simple R objects. These objects are stored in t h e current R workspace. To see what objects are currently defined type:
> objects ()
I This is equivalent to: > ls ()
I An object can be removed using the function remove or, equivalently, rm . > rm (x)
I To clear the workspace use: > rm (list = ls ())
Workspace
I To quit (exit) R use: > q ()