introduction to r: part i - introduction to the...

56
Introduction to R: Part I Introduction to the Environment Alexandre Perera i Lluna 1, 2 1 Centre de Recerca en Enginyeria Biomèdica (CREB) Departament d’Enginyeria de Sistemes, Automàtica i Informàtica Industrial (ESAII) Universitat Politècnica de Catalunya mailto:[email protected] 2 Centro de Investigación Biomédica en Red en Bioingeniería, Biomateriales y Nanomedicina (CIBER-BBN) Jan 2011 / Introduction to R (Universitat Rovira i Virgili)

Upload: others

Post on 05-Sep-2019

50 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

Introduction to R: Part IIntroduction to the Environment

Alexandre Perera i Lluna1,2

1Centre de Recerca en Enginyeria Biomèdica (CREB)Departament d’Enginyeria de Sistemes, Automàtica i Informàtica Industrial (ESAII)

Universitat Politècnica de Catalunyamailto:[email protected]

2Centro de Investigación Biomédica en Red en Bioingeniería, Biomateriales y Nanomedicina (CIBER-BBN)

Jan 2011 / Introduction to R (Universitat Rovira i Virgili)

Page 2: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Contents I

1 IntroductionStructure of the courseR Origins

2 So... What is R?R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

3 First stepsStarting and stopping RThe command lineR Frontends

4 Help! in RLocal environment help

Alexandre Perera i Lluna, Introduction to R: Part I

Page 3: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Contents II

Online Help

5 The WorkspaceWorkspace descriptionSaving /Loading the workspaceScripting

6 Extensions in RWhat is a package?Finding PackagesInstalling and loading packages

Alexandre Perera i Lluna, Introduction to R: Part I

Page 4: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Structure of the courseR Origins

Goals

Gain Skills for:

Import data into Renvironment

Explore and visualize data structure

Perform statistical analysis to a dataset

Perform basic regression and classification with R

Alexandre Perera i Lluna, Introduction to R: Part I

Page 5: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Structure of the courseR Origins

Main Contents

We will need to learn

What Rcan // Can not do

Rformal language and syntax

How to execute statistical tests and linear regression

How to seek help

Laboratory with real data (eigenfaces problem)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 6: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Structure of the courseR Origins

Lectures

Main course Body has six parts:

1 Introduction to the environment2 Introduction to Rlanguage and Visualization functions3 Statistics and linear regression with R4 Introduction to Pattern Recognition with R (Introduction to the Lab)5 Advanced topics and bibliography6 Lab guided session with world economics data

Alexandre Perera i Lluna, Introduction to R: Part I

Page 7: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Structure of the courseR Origins

Laboratory

We propose three labs:1 Introduction to R language, syntax and data visualization. (2-3h)2 Introduction to Pattern Recognition with R . (3h)3 Guided Lab with Penn World data (2-3 hours)4 Lab with Own data (or R included data). (2-3 h)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 8: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Structure of the courseR Origins

History

1976-1978: First version of S, born at Bell Laboratories. Ran on GCOSOperating System. Ported to UNIX in 1979

1983-1992: S3 language definition (New S, The Blue Book)

1983-1992: Statistical Modelling definition (The White Book)

1993: Robert Gentleman, Ross Ihaka write a short piece of software for the usein a lab. This software is published in the s-news mailing lists in August 1993.Implements much of the functionality of The White Book

1995: Martin Mächler from ETH Zurich convince authors to publish the sourceof the software. This finally happened under Free Software Foundation’s GNUgeneral license coverage

1995: Definition of the S3 language (The Green Book)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 9: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Structure of the courseR Origins

History

1996: Creation of the r-testers, r-announce, r-help and r-devel mailing lists

1997: Massive contribution from all the globe into Rextensions

1999: DSC - Directions in Statistical Computing Conference (DSC 1999,Vienna, Austria)

2004: International R User Conference (useR! 2004, Vienna, Austria)

2008: International R User Conference (useR! 2008, Dortmund, Germany,http://www.statistik.uni-dortmund.de/useR-2008//)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 10: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

What is R?

R ’s name comes from the name of two first authors (Robert Gentleman, RossIhaka). Also related with the language from Bell Labs S. R is strongly based onS

S is a high level language and environment for data analysis and graphicsgeneration

R fulfills much of S language definition. R can be considered as animplementation of S and is known as GNU S

Alexandre Perera i Lluna, Introduction to R: Part I

Page 11: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

If you use it, cite it!

R Development Core Team, R: A Language and Environment for StatisticalComputing, R Foundation for Statistical Computing, Vienna, Austria, 2008, ISBN:3-900051-07-0 , http://www.R-project.org

BibTexManual{R,

title = {R: A Language and Environment forStatistical Computing},

author = {{R Development Core Team}},organization = {R Foundation for Statistical Computing},address = {Vienna, Austria},year = 2008,note = {{ISBN} 3-900051-07-0},url = {http://www.R-project.org} }

Alexandre Perera i Lluna, Introduction to R: Part I

Page 12: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

R License

R is released under the GNU General Public License (GPL)

The cost of an R licence is of 0 Euros

Source code and function implementation are public. All main contributions arepublic

Its major cost is related to its learning curve...

NO WARRANTIES of any kind, no software company, no commercial support

Alexandre Perera i Lluna, Introduction to R: Part I

Page 13: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

R for Commercial use

Ir is the opinion of the developers that you can use ir for commercial use: (e.g.business and consulting)

GPL terms allow for any use to the tool

Re-distribution of Ror any software with contains code from R is restricted

Some extensions allow commercial use

Other extensions state “non-commercial use”, contact authors (sure you canpersuade them)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 14: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

R is not ..

Is not a general programming language (like in C, C++, Visual Basic, Java, etc)

Is not an interactive graphics generator (like in Matlab’s UI). Zooming isdifficult!

It is not a click and execute language (like in SPSS)

It is not like SPSS or Statistica

It is not a company product

It is not professionally documented

Alexandre Perera i Lluna, Introduction to R: Part I

Page 15: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

R is ..

A high level language for data management and storage

Language with large collection of predefined functions for matrix operation

High quality graphics generator

Strict syntax programming language

Similar to Matlab (As opposite to SPSS or Statistica)

Greater power as greater the knowledge of the language

Community made. Community maintained (world-wide)

Different from SPPS and alike.

Alexandre Perera i Lluna, Introduction to R: Part I

Page 16: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Pros & Cons

- Not easy without prior knowledge of programming languages

- Mainly a command line language

- S-type learning curve

+ Free, open source

+ Dynamic

+ Easy embeddable in other environments

+ Market driven (market is the developer)

+ Mailing lists (a must! http://www.r-project.org/mail.html)

+ Great Potential

Alexandre Perera i Lluna, Introduction to R: Part I

Page 17: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Need Interactive graphics?

Explore the community:http://code.google.com/p/playwith/

Alexandre Perera i Lluna, Introduction to R: Part I

Page 18: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Comparison with other statistical software I

From http://www.sciviews.org/benchmark/, Compares:1 R 1.9.0, the latest version of our reference software, a rich and powerful free “S

language dialect”. With ATLAS library optimized for Pentium IV.2 S-PLUS 6.1, the commercial equivalent of R3 Matlab 6.0 (R12), our previous reference4 O-Matrix 5.6, a cheap but very fast package, that can run most Matlab scripts5 Octave 2.1.42, a free “clone” of Matlab 4. With ATLAS library optimized for

Pentium IV.6 Scilab 2.7, a very complete free software, “not unlike” Matlab7 Ox 3.30, a very efficient matrix package similar to Gauss and free for academic

use

Alexandre Perera i Lluna, Introduction to R: Part I

Page 19: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Comparison with other statistical software II

I Matrix calculation: evaluates the ability of performing some common matrixcomputations.a creation, transposition, deformation of a 1500x1500 matrix. This test evaluates the

ability to create and manipulate matrices.b creation of a 800x800 normally distributed random matrix and taking the 1000th power

of all its elements. Evaluates the speed at which a random matrix is processed elementby element.

c sorting of 2,000,000 random values. Tests the speed of a sorting operation.d 700x700 cross-product matrix (b = a’ * a). Evaluates matrix operations.e linear regression over a 600x600 matrix (b = a b’). Tests the speed of execution for

linear models evaluation.

(from http://www.sciviews.org/benchmark/)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 20: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Comparison with other statistical software III

II Matrix functions: evaluates speed of some preprogrammed matrix functions.a fast Fourier transform over 800,000 values. Fourier transform is a commonly used

method in signal processing.b eigenvalues of a 320x320 random matrix. Eigenvalues are used in multivariate analyses

(PCA, ...).c determinant of a 650x650 random matrix. Calculation of the determinant of a matrix is

a common, but unequally optimized, function in matrix calculation packages.d cholesky decomposition of a 900x900 matrix. Another commonly preprogrammed

function.e inverse of a 400x400 random matrix. A computationally intensive function for which

various algorithms exist (with very different performances).

(from http://www.sciviews.org/benchmark/)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 21: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Comparison with other statistical software IV

III Programming: evaluates efficiency to run scripts and custom functions.a 750,000 Fibonacci numbers calculation. This evaluates the speed of vector calculation.b creation of a 2250x2250 Hilbert Matrix. Evaluates performances in matrix calculation

in scripts.c grand common divisors of 70,000 pairs. Tests potentials in using recursive functions.d creation of a 220x220 Toeplitz matrix. Check the speed of execution for loops.e Escoufier’s method on a 37x37 random matrix. Tests various aspects of programming

combined in a single test.

(from http://www.sciviews.org/benchmark/)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 22: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Comparison with other statistical software V

(from http://www.sciviews.org/benchmark/)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 23: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

What can be done with R: example I (Bioinformatics)

A crazy researcher wants to retrieve a DNA sequence for a gene. Transform thesequence to a numerical vector and compute the Fast Fourier Transform on a slidingwindow (spectrogram)

Involved steps

1 Connect to a sequence database2 Download a sequence and compute a

numerical representation3 Define a sliding window and compute a FFT4 Slide the window through the sequence5 Visualize the spectrogram

Involved steps

1 Database connection from R. Data downloadand format conversion

2 Sequence Parsing “GTCACCA”3 Signal processing tools4 Visualization tools (Spectrogram)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 24: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Databse integration (BioMart)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 25: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

code

> mart <- useMart(biomart="ensembl",+ dataset="hsapiens_gene_ensembl")# LDLR LDLR Human Low Density Lopoprotein Receptor

> gene <- getSequence(id = "3949", type="entrezgene",+ seqType = "transcript_exon_intron",mart = mart)> gene.info <- getGene(id ="3949", type="entrezgene",

mart=mart)> library( Biostrings)> gene.seq <- gene$transcript_exon_intron> dnas <- DNAString(gene.seq)> cdnas <- toComplex(dnas,

baseValues=c("A"=1+1i, "G"=0+1i,"T"=1-1i, "C"=0-1i))

> library(seewave)> spectdna <- spectro(as.matrix((cdnas[14000:18000])),

f=1, wl=60, plot=FALSE)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 26: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Visualization

plot(spectdna)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 27: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Example II. Finance

Goal

Find the maximum variation of TELEFONICA index in the period Jan 2007, March2008. Compare against the IBEX index.

Obtain values

Index visualization

Compute the derivative

Derivative visualization

Compute the max/min of the derivative

Alexandre Perera i Lluna, Introduction to R: Part I

Page 28: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

R talks to finance.yahoo.com

Yahoo! Finance provides with a historic finance data

Allows free access (not real-time, 15 minutes delay)

Yahoo! Finance download> library(tseries)> telefonica <- get.hist.quote("TEF.MC",+ start = "2006-01-01", end = "2008-05-07",+ quote = c("Open", "Volume"))

time series starts 2006-01-02

> ibex <- get.hist.quote("^IBEX",+ start = "2006-01-01", end = "2008-05-07",+ quote = c("Open", "Volume"))

time series starts 2006-01-02

Alexandre Perera i Lluna, Introduction to R: Part I

Page 29: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Index visualization

code> plot(telefonica)

1216

20

Ope

n

0.0e

+00

1.5e

+08

Vol

ume

2006 2007 2008

Index

telefonica

Alexandre Perera i Lluna, Introduction to R: Part I

Page 30: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Versus IBEX

code> plot(cbind(telefonica[, "Open"],+ ibex[, "Open"]), col = c("blue",+ "red"))

1216

20

tele

foni

ca...

.Ope

n..

1100

013

000

1500

0

ibex

....O

pen.

.

2006 2007 2008

Index

cbind(telefonica[, "Open"], ibex[, "Open"])

Alexandre Perera i Lluna, Introduction to R: Part I

Page 31: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Versus IBEX

code> pairs(cbind(telefonica[, "Open"],+ ibex[, "Open"]))

telefonica....Open..

11000 13000 15000

1214

1618

2022

●●●●●●●●●●

●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●

●●●

●●●●

●●●●

●●●● ●●●●●●●●●● ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●

●●●●● ●●●●●●●●●

●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●

●●●●●

●●●●●●●

●●●●●●●● ●●●●●●

●●●●●●●●●●

●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●

●●●●

●●●

●●●●●●

●●●●●●●●

●●●●●●●●●●●● ●●●●

●●●●●●●●

●●●●●●●●●

●●●●●●●●●●●

●●●●●●

●●●●●●●

●●●●●●●●●●●●●●●●

●●●●●

●●●●●●

●●● ●

●●●●●

●●●●●●●

●●●

●●●●●●

●●●●

●●

●●●●●●●●●●

●●●

●●●●

●●

●●●

●●●●●

●●●●●●

●●

●●

●●●

●●●●●●

●●●●

●●●●●●●●●

●●●●●

●●●●●

●●●●

●●

●●●●

●●

●●

●●

●●●●

●●●

●●●●●● ●

●●●●●●●

●●●●●

● ●●●●

●●● ●●

●●●●

●●

●●●●●

●●●

●●●●●

●●●●●●●●

●●●●

12 14 16 18 20 22

1100

013

000

1500

0

●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●

●●●●●●●

●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●

●●●●●●●

●●●

●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●

●●●●●●●●●●

●●●●●●●●●

●●●●●●●●

●●●●●●●●●

●●●●●

●●●●

●●●●●●

●●●●●

●●●●●●●●●●●●●●●

●●●●●

●●●●●●●●●●●●●

●●●●●●●

●●●●●●●

●●●

●●●●

●●

●●●

●●●

●●●●●●●●

●●●●●●●●●●

●●●●●●

●●●●●●●●

●●●●●●●●●●●●●●●●

●●●

●●●●

●●●●

●●●●●●●●●●

●●

●●●●●●●

●●●●●

●●●●

●●

●●

●●

●●●●

●●●●●●●

●●●

●●●●●

●●●● ●

●●

●●●●●●●●●

●●●●●●● ●

●●●

●●●

●●●●●

●●●●

●●●●

●●●

●●

●●●

●●●

●●●●●●

●●●●●

●●●

●●●

●●●●●●

●●

●● ●

●●●

●●

●●

●●●

●●●●

●●●●●

●●●

●●●●●●●

●●●

●●

●●●

●●●●

●●●●

●●

●●●●●

●●●

●●●●●

●●●●●●●●●

●●●

ibex....Open..

Alexandre Perera i Lluna, Introduction to R: Part I

Page 32: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Maximum/minimun Variation

code> plot(diff(telefonica$Open))> indmax <- which.max((diff(telefonica$Open)))> indmin <- which.min((diff(telefonica$Open)))> telefonica[indmax]

Open Volume2008-01-22 19 84981000

> telefonica[indmin]

Open Volume2008-01-21 21.18 70770700

Open Volume2008-01-22 19 84981000

Open Volume2008-01-21 21.18 70770700

−2.

0−

1.5

−1.

0−

0.5

0.0

0.5

1.0

Index

diff(

tele

foni

ca$O

pen)

2006 2007 2008

Figure: index derivative

Alexandre Perera i Lluna, Introduction to R: Part I

Page 33: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Example III: Sweave

This document itself!!! Latex + Sweave + R

R code is mixed with the presentation text: Sweave interprets the R code an dinserts latex output auto-magically

Alexandre Perera i Lluna, Introduction to R: Part I

Page 34: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Ejemplo III: Sweave

To generate this slide:

IRIS dataset> data(iris)> head(iris[, c(1, 2, 5)])

Sepal.Length Sepal.Width Species1 5.1 3.5 setosa2 4.9 3.0 setosa3 4.7 3.2 setosa4 4.6 3.1 setosa5 5.0 3.6 setosa6 5.4 3.9 setosa

> summary(iris[, c(1, 2, 5)])

Sepal.Length Sepal.WidthMin. :4.300 Min. :2.0001st Qu.:5.100 1st Qu.:2.800Median :5.800 Median :3.000Mean :5.843 Mean :3.0573rd Qu.:6.400 3rd Qu.:3.300Max. :7.900 Max. :4.400

Speciessetosa :50versicolor:50virginica :50

IRIS dataset plots> plot(iris, col = as.numeric(iris$Species),+ pch = 16)

Sepal.Length

2.0 3.0 4.0

●●

●●

●●

● ●●

●●

●●

● ●●●

●●

●●●

●●

●●

● ●

● ●●

●●

●●

●● ●

●●

●●

●●

●●●

● ●

●●

●●●●

●●●

●●

● ●●

●●

●●

● ●

●●

●●

● ●●

●●

●●●

●●

●●●

●●●

●●

●●

●●●●

●●

●●●●

●●

●●

●●●●

●●

●●●

●●

●●

●●

●●●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●

●●●

●●●

0.5 1.5 2.5

●●●●

●●

● ●●

●●

●●● ●●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●● ●

●●

●●

●●

●●●● ●

●●●●●

●●●

●●

●●●

●●

●●

● ●

●●

●●

●●●

●●

●●●

●●

●●

● ●●

●●

●●

4.5

6.0

7.5

●●●●

●●

●●●●

●●●

●●●●●●

●●

●●●

●●

●●

●●

●●●●

●●

●●

●●●

●●

●●●●●●●●●

●●●●●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●●●

●●

●●●

●●●

●●●●

2.0

3.0

4.0

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●●

●●

●●

●● ●

●●

● ●

●●

● ●●

●●

●●

●●

●●●

●●

●●●

●●

●Sepal.Width

●●●

●●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●●●

●●

●●

●●●●

●●●

●●

●●●

● ●

●●

●●●

●●● ●

●●

● ●●

●●

●●

●●

●●●

●●

●●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●●●

●●

●●

●●●●

●●●●

●●

●●●

● ●

●●

●●●

●●

●●

●●

●●●

●●

●●

●●

●●●

●●

● ●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●●●●

●●

●●

●●

●●

●●●●

●●

●●

●●●●

●●●●●●

●●●

●●

●●

●●●

●●●●

●●

●●●

●●

●●●●●

●●●

●●●●●

●●

●●●● ●●

● ●● ● ●●●● ●

●●●●● ●●

●●●●●●●● ●● ●●● ●●● ●●●●●●

● ●● ●●

●●●

●●● ●

●●

●●

●●●

●●

●●●●

●●●

●●● ●

●● ● ●

●●●

● ●●

●●● ●

●●●

●●

●●●

●●●●

●●

●●

●●

● ●●

●●

●●

●●

●●●●●

●●●●●●

●● ●● ●●

●●● ● ●●●● ●

●●●●●● ●

●●● ●●●●● ● ●●●

● ●●● ●●● ●●

●● ●● ●●

●●●

●●● ●

●●

●●

●●●

●●

● ●●●

● ●●

●●● ●

●● ●●

●●●

● ●●

● ●●●

●●●

●●

●● ●● ● ●●

●●

●●

● ●

● ●●

●●

●●

●●

●●●●●

●●●● ● ●●

Petal.Length

●●●●●●

●●●●●●●●●

●●●●●● ●

●●● ●●●●● ●●●●●●●●●●●●

●●

●●●●●

●●●

●●● ●

●●

●●

●●●

●●

●●●●● ●●

●●● ●

●●●●

●●●

● ●●

●●●●

●● ●

●●

●● ●● ●●●

●●

●●

●●

●●●●

●●

●●●●

●● ●

●●

● ●●●● ●

13

57

●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●

●●●●

●●

●●

●●●●●

●●●●●●●

●●●●

●●●●●●●●●●

●●●●

●●●

●●●

●●●●●●●

●●

●●

●●

●●●●

●●●●●●

●●●●●

●●●●●●●

0.5

1.5

2.5

●●●● ●●

●●●

●●●

●●●

●●● ●●

●●

●●●

●●●●●

●●●● ●

●● ●

●●●

●●

●●● ●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●●●

●●

●●● ●

●●

●●

●●

●●●

●●

●●

●●

●●

● ●●

●●●

●●

●●

●●

●●

●●

●●●

●●

●● ●● ●●

●●●

●●●

●●●

●●● ●●

●●

●●●●●●●

●●●● ●

●● ●

●●●

●●

●●● ●●

●●●

●●●

●●

●●

●●

●●

●●●●

●●

●●●

●●

●●

● ●●●

●●

●●

●●●

●●

●●

●●●

●●

●●

●●

●●

●●●

●● ●

●●

●●

●●

●●

●●

●●

●●

●●●●●●

●●●●●●●●

●●●●●●

●●

●●●●●●●●

●●●●●●●●●●●

●●

●●●●●

●●●

●●●

●●

●●

●●

●●

●●●●

●●

●●●●

●●●●

●●●●●

●●

●●●●

●●

●●

●●

●●●

●●●

●●

●●

● ●●

●●●

●●

●●

●●

●●

●●

●●

●●

Petal.Width

●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●

●●●●●●●●●●●

●●●●●●●

●●●●●●

●●

●●●●

●●

●●●●

●●

●●●●

●●●●●●●●●●●

●●●●●●

●●

●●

●●●

●●●●

●●

●●

●●●

●●●

●●●

●●

●●

●●

●●

●●●

●●

4.5 6.0 7.5

●●●● ● ●● ●● ● ●●●● ●●●● ●● ●●● ●●●●●●●● ●● ●●● ●●● ●●●● ●●● ●● ●●

●● ●● ●● ●● ●●● ●●●● ●●● ●● ●●●● ●●●●●●●● ●●● ● ●●●●● ●●● ●●● ●● ●

●● ●●● ●● ●● ●●● ●●● ●● ●●● ●● ●● ● ●●● ● ●● ●●●● ●●●● ●●●● ●●●●●●●

●● ●● ● ●●●● ● ●●●● ● ●●● ●●● ●●●●● ●●●●● ● ●●●● ●●● ●●● ● ● ●● ●● ●●

●●●● ●● ●● ●●● ●● ●● ●●●● ● ●●● ●●●● ●●●●● ●● ● ●●● ●●● ●●● ● ●●●● ●

●● ●●●●● ●● ●●● ●● ● ●● ●●● ●●●● ●●● ●● ●● ●●●● ● ●●●●●●● ●●●● ● ●●

1 3 5 7

●●●●●●●●●●●●●●●●●●●●●●● ●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●● ●●●● ●●● ●● ●● ●●●●● ●● ●●●●●●●●●●● ●●●●●●●●●●● ●●●●● ●

●● ●●● ●● ●●●●●●●●●● ●●● ●● ●● ●●●● ●●●●●● ● ●●●● ●●●● ●●●●●●●

●●●●● ●●●●●●●●●● ●●●●●● ●● ●●● ●●●●● ●●●●●●●●●●●● ●●●●●●●

●●●● ●● ●● ●●● ●● ●●●●● ●● ●● ●●●●● ●●●●● ● ●●●●●●●● ●●● ●●●●● ●

●● ●● ●●●●● ●●● ●● ●●● ●●● ●●●● ●●●● ●● ●● ●●● ●●●● ● ●●● ● ●●●● ●●

1.0 2.0 3.0

1.0

2.0

3.0

Species

Alexandre Perera i Lluna, Introduction to R: Part I

Page 35: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

R citationHow much R costs?Can I use R in my business ?What R is notWhat R isPros & ConsWhat can be done with R: examples

Ejemplo III: Sweave

in the tex file, I just wrote:

IRIS Dataset<<ejemploSweave,echo=TRUE>>=library(iris)head(iris)summary(iris)@

IRIS dataset Plots<<ejemploSweaveplot,echo=TRUE>>=plot(iris,col=iris$Species)x @

Alexandre Perera i Lluna, Introduction to R: Part I

Page 36: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Starting and stopping RThe command lineR Frontends

Starting and stopping R

In Windows Start Menu -> R-> R (Version) RGui.exeIn Unix, open a terminal and type “R”

Alexandre Perera i Lluna, Introduction to R: Part I

Page 37: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Starting and stopping RThe command lineR Frontends

The command line

Alexandre Perera i Lluna, Introduction to R: Part I

Page 38: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Starting and stopping RThe command lineR Frontends

R Commander

Rcmdr:

Disponible en http://socserv.mcmaster.ca/jfox/Misc/Rcmdr/

Can be installed from the package menu

Also from the command line:

install.packages("Rcmdr",dependencies=TRUE)

Activate with library(Rcmdr)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 39: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Starting and stopping RThe command lineR Frontends

R Commander

Alexandre Perera i Lluna, Introduction to R: Part I

Page 40: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Starting and stopping RThe command lineR Frontends

JGR

Java Gui for R http://www.rosuda.org

Alexandre Perera i Lluna, Introduction to R: Part I

Page 41: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Starting and stopping RThe command lineR Frontends

JGR

Java Gui for R http://www.rosuda.org

Alexandre Perera i Lluna, Introduction to R: Part I

Page 42: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Local environment helpOnline Help

Your first R command: “?”

?getwd

getwd package:base R DocumentationGet or Set Working DirectoryDescription:

'getwd' returns an absolute filename representing the currentworking directory of the R process; 'setwd(dir)' is used to setthe working directory to 'dir'.

Usage:getwd()setwd(dir)

Arguments:dir: A character string.

Value:'getwd' returns a character vector, or 'NULL' if the workingdirectory is not available.'setwd' returns the current directory before the change,invisibly. It will give an error if it does not succeed.

Note:These functions are not implemented on all platforms.

See Also:'list.files' for the _contents_ of a directory.

Examples:(WD <- getwd())if (!is.null(WD)) setwd(WD)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 43: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Local environment helpOnline Help

Help on help

?help

help {utils} R DocumentationDocumentation

Description

These functions provide access to documentation. Documentation on atopic with name name (typically, an R object or a data set) can bedisplayed with either help("name") or ?name.

Usage

help(topic, offline = FALSE, package = NULL,lib.loc = NULL, verbose = getOption("verbose"),try.all.packages = getOption("help.try.all.packages"),chmhelp = getOption("chmhelp"),htmlhelp = getOption("htmlhelp"),pager = getOption("pager"))

Alexandre Perera i Lluna, Introduction to R: Part I

Page 44: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Local environment helpOnline Help

Keyword based help

help.start() launches a browser (Unix)

apropos(“bessel”) finds names through substrings pattern matching. Returns allobjects containing the substring “bessel”.help.search() allows search on all local documentation trough keyword.Employs Fuzzy matching against:

FilenamealiasTitleConceptKeyword

Alexandre Perera i Lluna, Introduction to R: Part I

Page 45: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Local environment helpOnline Help

Online help

General: http://www.r-project.org/

Manuals: http://cran.r-project.org/manuals.html

FAQs: http://www.r-project.org/doc/FAQ/R-FAQ.htmlWiki: http://wiki.r-project.org/rwiki/doku.php

BooksCode snippetsGuidesGraph gallery with corresponding code

RSiteSearch("kalman", restrict="functions") launches abrowser and uses the search engine from http://www.r-project.org/

Alexandre Perera i Lluna, Introduction to R: Part I

Page 46: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Workspace descriptionSaving /Loading the workspaceScripting

Workspace

When data, matrices, variables and functions are defined, these live in theWorkspace

From Real Desktop Softwarehttp://www.real-desktop.de

Alexandre Perera i Lluna, Introduction to R: Part I

Page 47: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Workspace descriptionSaving /Loading the workspaceScripting

Workspace description

> ls()

[1] "ibex" "indmax"[3] "indmin" "iris"[5] "telefonica"

> profit <- 3000> loss <- 2000> ls()

[1] "ibex" "indmax"[3] "indmin" "iris"[5] "loss" "profit"[7] "telefonica"

Alexandre Perera i Lluna, Introduction to R: Part I

Page 48: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Workspace descriptionSaving /Loading the workspaceScripting

Workspace

> profit

[1] 3000

> rm(profit)> ls()

[1] "ibex" "indmax"[3] "indmin" "iris"[5] "loss" "telefonica"

> rm(list = ls())> ls()

character(0)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 49: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Workspace descriptionSaving /Loading the workspaceScripting

save(), save.image() and load()

> x <- 1> y <- 4> save(x, file = "x.RData")> save.image(file = "image.RData")> rm(x, y)> ls()

character(0)

> load(file = "x.RData")> ls()

[1] "x"

> load(file = "image.RData")> ls()

[1] "x" "y"

Alexandre Perera i Lluna, Introduction to R: Part I

Page 50: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Workspace descriptionSaving /Loading the workspaceScripting

sink()

sink("sink.txt")x <- 1:5outer(x, x, "*")sink()system("cat sink.txt")

[,1] [,2] [,3] [,4] [,5][1,] 1 2 3 4 5[2,] 2 4 6 8 10[3,] 3 6 9 12 15[4,] 4 8 12 16 20[5,] 5 10 15 20 25

Alexandre Perera i Lluna, Introduction to R: Part I

Page 51: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Workspace descriptionSaving /Loading the workspaceScripting

Writing Scripts

clicks in SPSS menus ⇒ command lines in R

You should organize commands (actions and steps) on a log-files or script fileregular text file)

Alexandre Perera i Lluna, Introduction to R: Part I

Page 52: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

Workspace descriptionSaving /Loading the workspaceScripting

Executing Scripts

Interactively (copy y paste)Running the complete script

> source("EjemploScript.R")

0 2 4 6 8

−1.

00.

5

x

y

Alexandre Perera i Lluna, Introduction to R: Part I

Page 53: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

What is a package?Finding PackagesInstalling and loading packages

packages

All functions and data in R are stored in packages

Only when a package is active (loaded) its contents are visible from Workspace

base: Standard functions in R , defined in standard R source

base functions allow for the built in functionality in R. This package is loadedautomatically when Rstarts upThere exists hundreds of packages, traditionally named contributed

Part of them (recommended) are usually included in the binary distribution of Rhttp://CRAN.R-project.org/⇐ General usehttp://www.bioconductor.org/⇐ Bioinformatics

Alexandre Perera i Lluna, Introduction to R: Part I

Page 54: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

What is a package?Finding PackagesInstalling and loading packages

library()

library() lists available packages in the local Rinstallation

To find out the contents of a package, use help()help(package=cluster)

Information on package 'cluster'

Description:

Package: clusterVersion: 1.11.10Date: 2008-02-29Priority: recommendedAuthor: Martin Maechler, based on S original by Peter

Rousseeuw <[email protected]>,[email protected] and

....

Index:

agnes Agglomerative Nestingclara Clustering Large Applicationsdaisy Dissimilarity Matrix Calculationdiana DIvisive ANAlysis Clustering

....

Alexandre Perera i Lluna, Introduction to R: Part I

Page 55: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

What is a package?Finding PackagesInstalling and loading packages

install.packages()

Installing new packages is easy if an Internet connection is present1 Locate the package names (e.g. colorRamps: create color tables)2 Install the package: install.packages('colorRamps')3 Load thel package: mediante library( 'colorRamps')

Obtain the contents of the package through help(package=colorRamps)

In Windows there is a menu entry for package management and installationPackages->install

Alexandre Perera i Lluna, Introduction to R: Part I

Page 56: Introduction to R: Part I - Introduction to the Environmentb2slab.upc.edu/wp-content/uploads/2014/02/CursR_I.pdf · Introduction to R: Part I Introduction to the Environment Alexandre

IntroductionSo... What is R?

First stepsHelp! in R

The WorkspaceExtensions in R

What is a package?Finding PackagesInstalling and loading packages

End of Part I

Alexandre Perera i Lluna, Introduction to R: Part I