presentation of r and r studio - normale sup › ~vorgogoz › bioinfo...presentation of r and r...
TRANSCRIPT
![Page 1: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/1.jpg)
Presentation of R and R Studio
Bénédicte Lefèvre – Club BioInfo08/11/2018
![Page 2: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/2.jpg)
What are R and R studio?
● R is a programming language created in 1993
● R and R studio are free, open source, software environment
● CRAN project: https://cran.r-project.org/
● R is useful to analyse data:
● sorting complexe data frame● statisics ● graphics● automatisation of repetitive tasks on datasets● and plenty of packages in function of your needs...
![Page 3: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/3.jpg)
R studio
![Page 4: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/4.jpg)
R studio
Script
Console
Environment
PlotsHelp
Package
![Page 5: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/5.jpg)
R studio
Script
Console
Environment
PlotsHelp
Package
Execute your code
Created objects
Show plotsLook for documentationInstall packages
![Page 6: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/6.jpg)
Before to start: let’s set our working directory
● Please, select your desktop as working directory and copy the iris.csv file in your desktop
![Page 7: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/7.jpg)
How to write a command and run it?
To run a command:- highlight it and click on runor- put your cursor in the line and press CTRL+enter
Lines that starts by # are not recognized as code but as comments
● Let’s try a few commands:
![Page 8: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/8.jpg)
How to create an object?
If you reassign a value to an existing object, the previous one is removed
To run a command:- highlight it and click on runor- put your cursor in the line and press CTRL+enter
● Let’s try a few commands:
![Page 9: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/9.jpg)
What can we do with the created objects?
R is case sensitive !x ≠ X
To run a command:- highlight it and click on runor- put your cursor in the line and press CTRL+enter
● Let’s try a few commands:
![Page 10: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/10.jpg)
A very important concept in R: vectors
● A vector is an object that contain a serie of variables of a same type, in a precise order
● c() is used to create vectors
E.Paradis, R for beginners, 2005
![Page 11: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/11.jpg)
Let’s create vectors and data frame: example with BigBang Theory
characters are written as ’’character’’ and appear in green
numbers appear in blue
![Page 12: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/12.jpg)
Visualisation of bigbang with View()
![Page 13: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/13.jpg)
Different kinds of objects and data
![Page 14: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/14.jpg)
Different kinds of objects and data
characters are written as ’’character’’ so ‘’45’’ is recognized as a character and not as a number
![Page 15: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/15.jpg)
Sort your data
sorting is done on the elements between brackets [ ]
data$col refers to the column named col of the table named data
![Page 16: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/16.jpg)
Sort your data
![Page 17: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/17.jpg)
Make a subtable
As we want a subtable with 2 dimensions, it is very important not to forget the comma in the brackets
![Page 18: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/18.jpg)
Navigate into your table: subscript
row number column number
![Page 19: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/19.jpg)
Navigate into your table
row number column number
![Page 20: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/20.jpg)
Navigate into your table
![Page 21: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/21.jpg)
Let’s now use functions
● Functions in R are written as following: function()
● You can specify options and arguments in function of your needs
● The arguments you do not specify will be set by default
E.Paradis, R for beginners, 2005
![Page 22: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/22.jpg)
Let’s now use functions
● Functions in R are written as following: function()
● You can specify option and arguments in function of your needs
● The arguments you do not specify will be set by default
![Page 23: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/23.jpg)
Syntax to write your function:myfunction=function(x) {what to do}
You can also create your own function
● Let’s write a function to calculate the age of the actors at the beginning of the serie, so twelve years ago
![Page 24: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/24.jpg)
You can also create your own function
● Let’s write a function to calculate the age of the actors at the beginning of the serie, so twelve years ago
![Page 25: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/25.jpg)
You can also create your own function
● Let’s write a function to calculate the age of the actors at the beginning of the serie, so twelve years ago
![Page 26: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/26.jpg)
Finding help in R
![Page 27: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/27.jpg)
Finding help in R
If you do not know the exact name of the function, use ??
![Page 28: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/28.jpg)
Finding help on the web
Do not ask questions on forums without doing preliminary research
●
Give a sample of data and script to illustrate your problem
● Books/ebooks● Emmanuel Paradis – R for beginners● Michael Crawley - The R book● Andrie de Vries & Joris Meys - R for dummies
● Websites● Statistical Tools for High-Throughput Data Analysis
(www.sthda.com)● Stackoverflow (http://stackoverflow.com)
● Forums● R-bloggers (www.r-bloggers.com)
![Page 29: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/29.jpg)
Export the data created on R in csv file
To easily find the pathway, you can right-clic on a document located in the place of interest and clic on properties, the pathway will be indicated
Alternatively to write the pathway, you can set your working directory manually:Session>Set working directory>choose location
![Page 30: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/30.jpg)
Import your own data in R studio
● A few precautions before to import a document
● Decimal numbers have to be written using a dot and not a comma: 2.5 instead of 2,5
● Do not mix empty cases and cases with NA in a same column
● Replace #VALUE ! (that happens in excell sheet when you apply function on Nas) by NA
● Convert your excell sheet in .csv format
![Page 31: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/31.jpg)
Import your own data in R studio
To easily find the pathway, you can right-clic on a document located in the place of interest and clic on properties, the pathway will be indicated
If you specified that the column separator of your csv file is tab, add the following argument:read.csv(‘’path/filename.csv’’, sep= ‘’\t’’)
● Iris dataset is a free dataset avaiable in R and regularly used in courses and examples
![Page 32: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/32.jpg)
Import your own data in R studio
![Page 33: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/33.jpg)
Remove the eventual NAs
is.na() return the cases that contain Nas
!is.na() make the contrary: it gives you the cases where there is something else than NA
![Page 34: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/34.jpg)
Create sub-tables in function of the specie
As you want a subtable and not a vector, do not forget the comma after the the specie
![Page 35: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/35.jpg)
Draw a plot
![Page 36: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/36.jpg)
Draw a nicer plot title
y and x subtitles
color and type of dot
limits of the y and x axis
![Page 37: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/37.jpg)
Draw the nicest plotuse the subtable
use points() to add data from other subtables
eventually add legend
![Page 38: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/38.jpg)
What about a boxplot?
![Page 39: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/39.jpg)
Save your plot
![Page 40: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/40.jpg)
other kinds of plots
Other usefull function linked to plots
split your plot window to see several plots (here 2x2)
add arrows or lines (angle=0) on your plots
add text on your plot
When you add elements to your plot, that are not between the axis, add the argument xpd=T to see it
![Page 41: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/41.jpg)
A few lines of statistics: is the sepal length different between species?
![Page 42: Presentation of R and R Studio - normale sup › ~vorgogoz › BioInfo...Presentation of R and R Studio Bénédicte Lefèvre – Club BioInfo 08/11/2018. What are R and R studio? R](https://reader034.vdocuments.net/reader034/viewer/2022052612/5f0c55797e708231d434e241/html5/thumbnails/42.jpg)
To conclude...
● Basic tools● Create, use, visualise vectors and data frames● Use and create functions● Find help● Draw and save plots and boxplots● Import and export your data● A few lines of statistics
● Many more possibilities in function of your needs● automatisation of repetitive tasks: for boucle and apply()● specific packages
● Practise !