Topic 1 - First steps

by Vianney DENIS

February 12, 2019

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:

Homepage here

Good resources

https://www.springer.com/series/6991?detailsPage=titles

Structure of R software

R consists of:

The core is the minimal installation and can be expanded using the add-on packages

Download and install R

Go to link for windows system or link 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.

Check for update

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.packages("abc")

It will tell you if your package is updated

To do only once, unless you remove or change of version of R

library("abc")

To do everytime: load your package

Find the correct package

Go to the link ), 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

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 is a very good read for beginners

Calling for help

?median # or
help(median)
median

Help on the function median

?median

It will give you details on how to use the function median

??median

It will give you all functions with median in their description. You can also find several active forums online.

Working directory

Important: use of working directory (wd) = file where you put all doc from a project

getwd() #Get Working Directory

Get the location of your current working directory

setwd() #Set Working Directory

Set up a new working directory. Also check “File>Change dir”.

Quit R

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

ls() # list

It will list all objects in memory

rm(list=ls())
# remove list

It will remove all objects in memory

Tip: Use CTRL+L to clean your screen

R is a calculator

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:

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

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)

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

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

For this class, and especially if you are beginner in R you must use use R studio. 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