--- title: "Topic 1 - First steps" author: "by Vianney DENIS" date: "February 12, 2019" output: "html_document": theme: cerulean highlight: haddock --- ### Website All documents for this class will be available at https://www.dipintothereef.com/. You need me to create you an access. I may have some trouble sometimes to upload everything, so sorry in advance if there is some delay. ### What is R? R is: + a programming language + an environment for statistics (software) + open source software (free for everybody to use) + on cutting age: close to 4,000 third party add-ons Homepage [here](http://www.r-project.org) ### R gallery https://www.r-graph-gallery.com/all-graphs/ ### Good resources https://www.springer.com/series/6991?detailsPage=titles ### Structure of R software R consists of: + core + add-on packages (CRAN, Bioconductor) The core is the minimal installation and can be expanded using the add-on packages ### Download and install R Go to [link](http://cran.r-project.org/bin/windows/base/) for windows system or [link](https://cran.r-project.org/) for mac. Run the installer, and follow the instructions, untick *'Save version number in registry'* R is a language, what you are installing is a graphical user interface (GUI) = a way to communicate. You have several GUI using R langage, the one you install is be default. We will later use [R studio](https://www.rstudio.com/). ### Check for update ```{r, eval=FALSE} if(!require(installr)) { install.packages("installr"); require(installr)} updateR() ``` This will start the updating process of your R installation. It will check for newer versions, and if one is available, will guide you through the decisions you need to make It will tell you if your R version is out-of-date ### Install and use a package Can be done using either using the menu "Packages" or by writing following code chunks: + Install the package ```abc``` ```{r, eval=FALSE} install.packages("abc") ``` It will tell you if your package is updated To do only once, unless you remove or change of version of R + Use the package ```abc``` ```{r, eval=FALSE} library("abc") ``` To do everytime: load your package ### Find the correct package Go to the [link](http://cran.r-project.org/web/views/) ), which use task views. It groups packages by subject areas (Environmetrics, multivariate, etc.) Package are maintained regularly by authors, if not they are excluded from CRAN archives. ### Practice 1 > * Install and load the package * ```vegan``` ### Help + R comes with built-in manuals and helps files Third-party manuals guides and books are available at http://cran.r-project.org/other-docs.html, but frozen and frozen and no longer actively maintained [R for beginner](https://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf) is a very good read for beginners + Using "Help > Html help", look for some help on the function ```median``` ### Calling for help ```{r, eval=FALSE} ?median # or help(median) median ``` Help on the function ```median``` ```{r, eval=FALSE} ?median ``` It will give you details on how to use the function ```median``` ```{r, eval=FALSE} ??median ``` It will give you all functions with median in their description. You can also find several active [forums](http://stats.stackexchange.com) online. ### Working directory **Important**: use of working directory (wd) = file where you put all doc from a project ```{r, eval=FALSE} getwd() #Get Working Directory ``` Get the location of your current working directory ```{r, eval=FALSE} setwd() #Set Working Directory ``` Set up a new working directory. Also check "File>Change dir". ### Quit R ```{r, eval=FALSE} q() ``` It will leave your R session, do not save your workspace otherwise it will save all objects your create in R memory (.Rdata file). It will also save all commands given during the session (.Rhistory file). Those files can be opended by "File>Load workspace and file > Load history" ### List objects ```{r, eval=FALSE} ls() # list ``` It will list all objects in memory ```{r, eval=FALSE} rm(list=ls()) # remove list ``` It will remove all objects in memory **Tip**: Use CTRL+L to clean your screen ### R is a calculator ```{r, eval=FALSE} 3+2 # addition 3-2 # substraction 3*2 # multiplication 3/2 # division 3^3 # power log(2) # logarithm exp(2) # exponential (5 + 3) / 4 # define priority using () or {} pi*4 # common function ``` R is case sensitive ```pi``` will work, whereas ```Pi``` will not ### Text editor Using "File>New Script"", open a script editor This is the simple and built-in R script editor, you have plainty of alternatives. If R crash you may lose everything. You can write you code inside, select the lines, and send them to the console using "Run lines or selection" **Alternatives**: Notepad (Ctrl C + V), Tinn R, Sublime, WinEdt package ```rite```, R Studio, and many others. ### Data preparation **Always, always, always** keep simple: variable in column, observation in rows No format, no color, no merging, no fancy decoration: be boring! Format: most format can be read. ```.txt``` or ```.csv``` files represent usually the best choices ### Reading data Using the built-in script editor of R, you can practice importing data: ```{r, eval=FALSE} setwd ('D:/.../dataset')# set your working directory read.table ("taiwan_coral.txt",header = TRUE,sep="\t",dec=".")# opening a simple datasheet ``` You can also target directly your file without setting up your working directory ```{r, eval=FALSE} read.table ("D:/.../Class 1/taiwan_coral.txt",header = TRUE,sep="\t", dec=".")# long version read.table("taiwan_coral.txt", TRUE, "\t",".")# you can also shorten some part once you get use to it, but be careful when using this ``` Your data are not stored in R memory ### Storing, importing data Now you can store it in R using the operator ```<-```or ```=``` (to avoid at first) ```{r, eval=FALSE} taiwan_coral <- read.table ("taiwan_coral.txt", header = TRUE, sep="\t", dec=".") ``` ```read.delim``` or ```read.csv``` and many others are alternative to ```read.table``` Now, you can call your data by calling your object ```taiwan_coral``` ```{r, eval=FALSE} taiwan_coral ``` Always think about the name of your file, if it has column **titles**, the **separator** (tab, space, comma, semi-colon), the character use for **decimals** (dot, comma) **CONGRATS**, this is your first script in R ### Foreign files + Using the package ```foreign```: SPSS, SAS + Using the package ```sas7bdat```: SAS + etc. ### ![](https://www.rstudio.com/wp-content/uploads/2016/09/RStudio-Logo-Blue-Gray-125.png) For this class, and especially if you are beginner in R you must use use [R studio](https://www.rstudio.com/). This will be the easiest for me to debug you if you have any problem. ### Practice 2 > * Using the built-in script editor of R Studio ("File>New File>R Script"):* *1) change your working directory to the one you created for this class* *2) import the file ```rairuoho.txt``` (see below) in an object called rairuoho (extra: try importing the url of the file using:```read.table```)* *3) using the help facilities, find out, how you could read the first 5 lines only (using the ```read.table``` function) * *4) ```sum``` for this table, values from the 2nd column ```rairuoho``````[``` no. line```,``` no. column```]``` * * 5) 'Knit' your script ("File>Knit Document") in `html` format and upload it below*