What is R?

Is a computer language that allows the user to program algorithms and use tools that have been programmed by others. R is based on the computer language S, which was developed by John Chambers and others at Bell Laboratories in 1976. In the early 1990s, Ross Ihaka and Robert Gentleman (University of Auckland, in Auckland, New Zealand) experimented with the language and called their product R. Note that both their first names begin with the letter R. Also when modifying a language called S, it is tempting to call it T or R.

R comes with a series of default packages. A package is a collection of previously programmed functions, often including functions for specific tasks. It is tempting to call this a library, but the R community refers to it as a package.


Change appearance

Tools -> Global options… -> Appearance


>

Significa “What now?”
The screen prompt “>” is an invitation to put R to work.
Each line can have at most 128 characters.
When continuation is expected, the prompt changes from > to +
Note that the + continuation prompt does not carry out arithmetic plus.


How cite R?

citation()
## 
## To cite R in publications use:
## 
##   R Core Team (2022). R: A language and environment for statistical
##   computing. R Foundation for Statistical Computing, Vienna, Austria.
##   URL https://www.R-project.org/.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {R: A Language and Environment for Statistical Computing},
##     author = {{R Core Team}},
##     organization = {R Foundation for Statistical Computing},
##     address = {Vienna, Austria},
##     year = {2022},
##     url = {https://www.R-project.org/},
##   }
## 
## We have invested a lot of time and effort in creating R, please cite it
## when using it for data analysis. See also 'citation("pkgname")' for
## citing R packages.


Cite specific packages?

citation('vegan')
## 
## To cite package 'vegan' in publications use:
## 
##   Oksanen J, Simpson G, Blanchet F, Kindt R, Legendre P, Minchin P,
##   O'Hara R, Solymos P, Stevens M, Szoecs E, Wagner H, Barbour M,
##   Bedward M, Bolker B, Borcard D, Carvalho G, Chirico M, De Caceres M,
##   Durand S, Evangelista H, FitzJohn R, Friendly M, Furneaux B, Hannigan
##   G, Hill M, Lahti L, McGlinn D, Ouellette M, Ribeiro Cunha E, Smith T,
##   Stier A, Ter Braak C, Weedon J (2022). _vegan: Community Ecology
##   Package_. R package version 2.6-2,
##   <https://CRAN.R-project.org/package=vegan>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {vegan: Community Ecology Package},
##     author = {Jari Oksanen and Gavin L. Simpson and F. Guillaume Blanchet and Roeland Kindt and Pierre Legendre and Peter R. Minchin and R.B. O'Hara and Peter Solymos and M. Henry H. Stevens and Eduard Szoecs and Helene Wagner and Matt Barbour and Michael Bedward and Ben Bolker and Daniel Borcard and Gustavo Carvalho and Michael Chirico and Miquel {De Caceres} and Sebastien Durand and Heloisa Beatriz Antoniazi Evangelista and Rich FitzJohn and Michael Friendly and Brendan Furneaux and Geoffrey Hannigan and Mark O. Hill and Leo Lahti and Dan McGlinn and Marie-Helene Ouellette and Eduardo {Ribeiro Cunha} and Tyler Smith and Adrian Stier and Cajo J.F. {Ter Braak} and James Weedon},
##     year = {2022},
##     note = {R package version 2.6-2},
##     url = {https://CRAN.R-project.org/package=vegan},
##   }
citation('ggplot2')
## 
## To cite ggplot2 in publications, please use:
## 
##   H. Wickham. ggplot2: Elegant Graphics for Data Analysis.
##   Springer-Verlag New York, 2016.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Book{,
##     author = {Hadley Wickham},
##     title = {ggplot2: Elegant Graphics for Data Analysis},
##     publisher = {Springer-Verlag New York},
##     year = {2016},
##     isbn = {978-3-319-24277-4},
##     url = {https://ggplot2.tidyverse.org},
##   }
citation('randomForest')
## 
## To cite randomForest in publications use:
## 
##   A. Liaw and M. Wiener (2002). Classification and Regression by
##   randomForest. R News 2(3), 18--22.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Article{,
##     title = {Classification and Regression by randomForest},
##     author = {Andy Liaw and Matthew Wiener},
##     journal = {R News},
##     year = {2002},
##     volume = {2},
##     number = {3},
##     pages = {18-22},
##     url = {https://CRAN.R-project.org/doc/Rnews/},
##   }


Help

?lm


Simbols (P: 22)

+ - * / ^ arithmetic
> >= < <= == != relational
! & logical
~ model formulae
<- -> assignment
$ list indexing (the ‘element name’ operator)
: create a sequence


Equality

TRUE==TRUE
## [1] TRUE
'HELLO'=='GOOBYE'
## [1] FALSE
'HELLO'=='HELLO'
## [1] TRUE
3==2
## [1] FALSE
3==3
## [1] TRUE


Inequality

TRUE!=TRUE
## [1] FALSE
'HELLO'!='GOOBYE'
## [1] TRUE


Change directory

setwd ("C:/users/Pablo Guti/desktop/")


If you want to find out the name of the current working directory, use:

getwd()
## [1] "C:/Users/pgutierr/OneDrive - University of Vermont/Curriculum/19_ Personal webpage/TropicalFreshwaterEcology/_pages/Lectures/Multv.Stat"


Instals packages

install.packages()
install.packages()
install.packages()


Instals libraries

library( )
library( )
library( )


Exercises

5+6+3+6+4+2+4+8+3+2+7
## [1] 50
2+3; 5*7; 3-7
## [1] 5
## [1] 35
## [1] -4
pi
## [1] 3.141593
sin(pi/2)
## [1] 1
cos(pi/2)
## [1] 6.123032e-17


Exponentes

1.2e3
## [1] 1200
1e4
## [1] 10000
3e4
## [1] 30000


Redondear

floor(5.7)
## [1] 5
ceiling(5.7)
## [1] 6


c()function, where c stands for concatenate

Wingcrd <- c(59, 55, 53.5, 55, 52.5, 57.5, 53, 55)
Wingcrd
## [1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0
Wingcrd [3]
## [1] 53.5
Wingcrd [1 : 5]
## [1] 59.0 55.0 53.5 55.0 52.5
Wingcrd [-2]
## [1] 59.0 53.5 55.0 52.5 57.5 53.0 55.0
sum(Wingcrd)
## [1] 440.5
s.win <- sum(Wingcrd)
s.win
## [1] 440.5


Tarsus <- c(22.3, 19.7, 20.8, 20.3, 20.8, 21.5, 20.6,21.5)
Head <- c(31.2, 30.4, 30.6, 30.3, 30.3, 30.8, 32.5,NA)
Wt <- c(9.5, 13.8, 14.8, 15.2, 15.5, 15.6, 15.6,15.7)

Bird<-c(Tarsus,Head,Wt)
Bird
##  [1] 22.3 19.7 20.8 20.3 20.8 21.5 20.6 21.5 31.2 30.4 30.6 30.3 30.3 30.8 32.5
## [16]   NA  9.5 13.8 14.8 15.2 15.5 15.6 15.6 15.7


cbind()function, combines the variables in such a way that the output

contains the original variables in columns.

Bird1 <- cbind(Tarsus, Head, Wt)
Bird1
##      Tarsus Head   Wt
## [1,]   22.3 31.2  9.5
## [2,]   19.7 30.4 13.8
## [3,]   20.8 30.6 14.8
## [4,]   20.3 30.3 15.2
## [5,]   20.8 30.3 15.5
## [6,]   21.5 30.8 15.6
## [7,]   20.6 32.5 15.6
## [8,]   21.5   NA 15.7


similar to the cbind function to arrange the variables in columns,

the rbind function combines the data in rows.

Bird2 <- rbind(Tarsus, Head, Wt)
Bird2
##        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## Tarsus 22.3 19.7 20.8 20.3 20.8 21.5 20.6 21.5
## Head   31.2 30.4 30.6 30.3 30.3 30.8 32.5   NA
## Wt      9.5 13.8 14.8 15.2 15.5 15.6 15.6 15.7


Remover NA

x<-c(1:8,NA)
mean(x)
## [1] NA
x
## [1]  1  2  3  4  5  6  7  8 NA


Head <-c(31.2,30.4,30.6,30.3,30.8,NA,32.5,NA)
Head
## [1] 31.2 30.4 30.6 30.3 30.8   NA 32.5   NA
sum(Head)
## [1] NA
sum(Head, na.rm=TRUE)
## [1] 185.8


na.rm=TRUE

mean(x,na.rm=T)
## [1] 4.5


Convertir NA en Ceros

vmv<-c(1:6,NA,NA,9:12)
vmv
##  [1]  1  2  3  4  5  6 NA NA  9 10 11 12
which(is.na(vmv))
## [1] 7 8
vmv[is.na(vmv)]<- 0
vmv
##  [1]  1  2  3  4  5  6  0  0  9 10 11 12


Asigna un determinado elemento

A<-1:10
A
##  [1]  1  2  3  4  5  6  7  8  9 10
B<-c(2,4,8)
B
## [1] 2 4 8
mean(A)
## [1] 5.5
mean(B)
## [1] 4.666667
max(B)
## [1] 8
min(A)
## [1] 1
sum(A)
## [1] 55


If you want to begin a new analysis on a different dataset, it may be useful to

remove all variables. One option is to quit R and restart it. Alternatively,

rm(list = ls(all = TRUE))


Import data (.csv, .txt, direct import)

xxx.frm=read.table("xxx.txt",header=T)
attach(xxx.frm)
xxx.frm

xxx.frm=read.csv("xxx.csv")
attach(xxx.frm)
xxx.frm

data<-read.table(file.choose(),header=T)


References:

Crawley, M. J. (2012). The R book. John Wiley & Sons.