by Vianney DENIS
February 12, 2019
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.
R is:
Homepage here
R consists of:
The core is the minimal installation and can be expanded using the add-on packages
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.
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
Can be done using either using the menu “Packages” or by writing following code chunks:
abc
It will tell you if your package is updated
To do only once, unless you remove or change of version of R
abc
To do everytime: load your 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.
Install and load the package
vegan
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
median
Help on the function median
It will give you details on how to use the function median
It will give you all functions with median in their description. You can also find several active forums online.
Important: use of working directory (wd) = file where you put all doc from a project
Get the location of your current working directory
Set up a new working directory. Also check “File>Change dir”.
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”
It will list all objects in memory
It will remove all objects in memory
Tip: Use CTRL+L to clean your screen
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
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.
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
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
Now you can store it in R using the operator <-
or =
(to avoid at first)
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
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
Using the package foreign
: SPSS, SAS
Using the package sas7bdat
: SAS
etc.
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.
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 filerairuoho.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 theread.table
function)
4)sum
for this table, values from the 2nd columnrairuoho
[
no. line,
no. column]
5) ‘Knit’ your script (“File>Knit Document”) inhtml
format and upload it below