SlideShare une entreprise Scribd logo
1  sur  37
Getting started with R Jacob van Etten
What is R? “R is a free software environment for statistical computing and graphics.” www.r-project.org
Why R? R takes time to learn and use. So why should I bother? There are more user-friendly  programmes, right?
12 reasons to learn R 1. Rigour and strategy in data analysis – not “thinking after clicking”. 2. Automatizing repeated calculations can save time in the end. 3. A lot of stuff is simply not feasible in menu driven software.
12 reasons to learn R 4. R is not only about software, it is also an online community of user support and collaboration. 5. Scripts make it easy to communicate about your problem. Important for collaborative research! 6. Research becomes replicable when scripts are published.
12 reasons to learn R 7. R packages represent state-of-the-art in many academic fields.  8. Graphics in R are very good. 9. R stimulates learning – graduation from user to developer.
12 reasons to learn R 10. R is free, which saves you money. Or R may be the only option when budgets are restricted. 11. R encourages to freely explore new methods and learn about them. 12. Knowing to work with R is a valuable and transferable skill.
It’s the long way but it’s worth it...
Resources to learn R My two picks for you http://pj.freefaculty.org/R/Rtips.html
Some R packages of interest RNCEP – Reanalysis data clim.pact– Downscaling climate data GhcnDaily – dailyweather data weatherData(R-Forge) – Daily weather data and derived bioclimatic variables relevant to plant growth raster – gives access to WorldClim data
And a lot more here... http://cran.r-project.org/other-docs.html
Downloading R
Choose a mirror nearby and then... Binaries “When downloading, a completely functional program without any installer is also often called program binary, or binaries (as opposed to the source code).” (Wikipedia)
And finally,you can download R... When downloading has finished, run the installer
The bare R interface
RStudio makes life easier Rstudio.org
Four parts Create a new R script: File – New – R Script Scripts, documentation Workspace/history Files, plots, packages and help Console
Our first code... Type 1 + 1 into the script area. Then click “Run”. What happens?
Exercises: running code Type a second line with another calculation (use “-”, “/”, or “*”) and click “Run” again. Select only one line with the mouse or Shift + arrows. Then click “Run”. Save your first code to a separate folder “Rexercises”.
Following exercises In the next exercises, we will develop a script. Just copy every new line and add it to your script, without erasing the previous part. If you want to make a comment in your script, put a # before that line. Like this: #important to remember: use # to comment
If the exercises are a bit silly... ...that’s because you are learning.
Vector Type a new line with the expression 1:10 in the script and run this line. A concatenation of values is called a  vector.
Making a new variable If we send 1:10 into the console it will only print the outcome. To “store” this vector, we need to do the following. a <- 1:10 new variable “a”      assign       vector values 1 to 10
Operations with vectors Try the following and see what happens. a a * 2 a * a b <- a * a b print(b)
Other ways of making vectors d <- c(1, 6, 9) d class(d) f <- LETTERS f class(f) What is the difference between d and f?
Functions Actually, we have already seen functions! Functions consist of a name followed by one or more arguments. Commas and brackets complete the expression. class(f) c(d,f) name    argument
Cheat sheet When you use R, you will become familiar with the most common functions. If you need a less common function, there are ways to discover the right one. For now, use the cheat sheet to look up the functions you need.
Getting help on functions This will open help pages for the functions in your browser. ?c ?class Especially the examples are often helpful.  Just copy and paste the code into the console and see with your own eyes what happens!
Matrices We have already met the vector. If we put two or more vector together as columns, we get a matrix. X <- c(1,2,3) Y <- c(8,9,7) Z <- c(4,2,8) M <- cbind(X, Y, Z) How many columns and rows does M have?
Data frames Matrices must consist of values of the same class. But often datasets consist of a mix of different types of variables (real numbers and groups). This is the job of data frames. L <- c(“a”, “b”, “c”) Df <- data.frame(X,Y,Z,L) Visualize Df like this:  str(Df) What would happen if you tried to make a matrix out of these same vectors instead? Try and see.
Getting data into R ?read.csv CSV files are a relatively trouble-free way of getting data into R. It is a fairly common format. You can make a CSV file in any spreadsheet software.
Create a CSV file Add your own favorite actor, too. Open the file with Notepad.  Make sure the values are separated by commas.
Now use R to read it Now read it into R. actors <- read.csv(yourfile.csv) str(actors)
Subsetting There are many ways of selecting only part of a data frame. Observe carefully what happens. actors[1:2,] actors[,1:2] actors[“Age”] actors[c(“Name”, “Age”)] subset(actors, Age> 40) Now create a new data frame with the actors younger than 45.
Graphics The plot function makes graphs. plot(actors[c(“sex”, “Age”)])
Summary You now know about: Variables Functions Vectors Matrices Data frames Getting tabular data into R Subsetting Simple plotting
Time for your first fight...

Contenu connexe

Tendances

Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonRalf Gommers
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueGhaffar Khan
 
R programming Language , Rahul Singh
R programming Language , Rahul SinghR programming Language , Rahul Singh
R programming Language , Rahul SinghRavi Basil
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2Self-Employed
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python C. ASWINI
 
AjayBhullar_Resume (5)
AjayBhullar_Resume (5)AjayBhullar_Resume (5)
AjayBhullar_Resume (5)Ajay Bhullar
 
Introduction to R - from Rstudio to ggplot
Introduction to R - from Rstudio to ggplotIntroduction to R - from Rstudio to ggplot
Introduction to R - from Rstudio to ggplotOlga Scrivner
 
Tutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plotsTutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plotsAvjinder (Avi) Kaler
 
Ii pu cs practical viva voce questions
Ii pu cs  practical viva voce questionsIi pu cs  practical viva voce questions
Ii pu cs practical viva voce questionsProf. Dr. K. Adisesha
 
Unit 2 linked list
Unit 2   linked listUnit 2   linked list
Unit 2 linked listDrkhanchanaR
 
Five python libraries should know for machine learning
Five python libraries should know for machine learningFive python libraries should know for machine learning
Five python libraries should know for machine learningNaveen Davis
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library FunctionDeepak Singh
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceTransweb Global Inc
 

Tendances (20)

Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for Python
 
Clean code
Clean codeClean code
Clean code
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
R programming Language , Rahul Singh
R programming Language , Rahul SinghR programming Language , Rahul Singh
R programming Language , Rahul Singh
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Unit v
Unit vUnit v
Unit v
 
Thin Template Explained
Thin Template ExplainedThin Template Explained
Thin Template Explained
 
AjayBhullar_Resume (5)
AjayBhullar_Resume (5)AjayBhullar_Resume (5)
AjayBhullar_Resume (5)
 
Introduction to R - from Rstudio to ggplot
Introduction to R - from Rstudio to ggplotIntroduction to R - from Rstudio to ggplot
Introduction to R - from Rstudio to ggplot
 
Tutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plotsTutorial for Circular and Rectangular Manhattan plots
Tutorial for Circular and Rectangular Manhattan plots
 
Ii pu cs practical viva voce questions
Ii pu cs  practical viva voce questionsIi pu cs  practical viva voce questions
Ii pu cs practical viva voce questions
 
Data visualization with R
Data visualization with RData visualization with R
Data visualization with R
 
Unit 2 linked list
Unit 2   linked listUnit 2   linked list
Unit 2 linked list
 
Five python libraries should know for machine learning
Five python libraries should know for machine learningFive python libraries should know for machine learning
Five python libraries should know for machine learning
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
 
Lecture 2c stacks
Lecture 2c stacksLecture 2c stacks
Lecture 2c stacks
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer Science
 
ch13
ch13ch13
ch13
 

Similaire à Getting started with R

Presentation on use of r statistics
Presentation on use of r statisticsPresentation on use of r statistics
Presentation on use of r statisticsKrishna Dhakal
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiUnmesh Baile
 
Data Science - Part II - Working with R & R studio
Data Science - Part II -  Working with R & R studioData Science - Part II -  Working with R & R studio
Data Science - Part II - Working with R & R studioDerek Kane
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfKabilaArun
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfattalurilalitha
 
Reproducibility with R
Reproducibility with RReproducibility with R
Reproducibility with RMartin Jung
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptanshikagoel52
 
Data analysis in R
Data analysis in RData analysis in R
Data analysis in RAndrew Lowe
 
An introduction to R is a document useful
An introduction to R is a document usefulAn introduction to R is a document useful
An introduction to R is a document usefulssuser3c3f88
 
Introduction to Data Science With R Notes
Introduction to Data Science With R NotesIntroduction to Data Science With R Notes
Introduction to Data Science With R NotesLakshmiSarvani6
 
R programming presentation
R programming presentationR programming presentation
R programming presentationAkshat Sharma
 

Similaire à Getting started with R (20)

Presentation on use of r statistics
Presentation on use of r statisticsPresentation on use of r statistics
Presentation on use of r statistics
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbai
 
Unit 3
Unit 3Unit 3
Unit 3
 
Data Science - Part II - Working with R & R studio
Data Science - Part II -  Working with R & R studioData Science - Part II -  Working with R & R studio
Data Science - Part II - Working with R & R studio
 
Lecture_R.ppt
Lecture_R.pptLecture_R.ppt
Lecture_R.ppt
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
Reproducibility with R
Reproducibility with RReproducibility with R
Reproducibility with R
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
Data analysis in R
Data analysis in RData analysis in R
Data analysis in R
 
An introduction to R is a document useful
An introduction to R is a document usefulAn introduction to R is a document useful
An introduction to R is a document useful
 
Basics of R
Basics of RBasics of R
Basics of R
 
3.5
3.53.5
3.5
 
effectivegraphsmro1
effectivegraphsmro1effectivegraphsmro1
effectivegraphsmro1
 
Introduction to Data Science With R Notes
Introduction to Data Science With R NotesIntroduction to Data Science With R Notes
Introduction to Data Science With R Notes
 
R programming presentation
R programming presentationR programming presentation
R programming presentation
 

Plus de CCAFS | CGIAR Research Program on Climate Change, Agriculture and Food Security

Plus de CCAFS | CGIAR Research Program on Climate Change, Agriculture and Food Security (20)

CGIAR-AICCRA Knowledge Management Guide (2021)
CGIAR-AICCRA Knowledge Management Guide (2021)CGIAR-AICCRA Knowledge Management Guide (2021)
CGIAR-AICCRA Knowledge Management Guide (2021)
 
Achieving NDC Ambition in Agriculture: How much does agriculture contribute t...
Achieving NDC Ambition in Agriculture: How much does agriculture contribute t...Achieving NDC Ambition in Agriculture: How much does agriculture contribute t...
Achieving NDC Ambition in Agriculture: How much does agriculture contribute t...
 
Achieving NDC Ambition in Agriculture: Mitigation ambition in new & updated N...
Achieving NDC Ambition in Agriculture: Mitigation ambition in new & updated N...Achieving NDC Ambition in Agriculture: Mitigation ambition in new & updated N...
Achieving NDC Ambition in Agriculture: Mitigation ambition in new & updated N...
 
Achieving NDC Ambition in Agriculture: Overview of NDC ambition in the agricu...
Achieving NDC Ambition in Agriculture: Overview of NDC ambition in the agricu...Achieving NDC Ambition in Agriculture: Overview of NDC ambition in the agricu...
Achieving NDC Ambition in Agriculture: Overview of NDC ambition in the agricu...
 
CCAFS and GRA Resources for CLIFF-GRADS 2021
CCAFS and GRA Resources for CLIFF-GRADS 2021CCAFS and GRA Resources for CLIFF-GRADS 2021
CCAFS and GRA Resources for CLIFF-GRADS 2021
 
CSA Monitoring: Understanding adoption, synergies and tradeoffs at farm and h...
CSA Monitoring: Understanding adoption, synergies and tradeoffs at farm and h...CSA Monitoring: Understanding adoption, synergies and tradeoffs at farm and h...
CSA Monitoring: Understanding adoption, synergies and tradeoffs at farm and h...
 
Livestock and sustainability in changing climate: Impacts and global best pra...
Livestock and sustainability in changing climate: Impacts and global best pra...Livestock and sustainability in changing climate: Impacts and global best pra...
Livestock and sustainability in changing climate: Impacts and global best pra...
 
Plant-based protein market in Asia
Plant-based protein market in AsiaPlant-based protein market in Asia
Plant-based protein market in Asia
 
ADB ESLAP case study outputs and synthesis results: Sustainable livestock gui...
ADB ESLAP case study outputs and synthesis results: Sustainable livestock gui...ADB ESLAP case study outputs and synthesis results: Sustainable livestock gui...
ADB ESLAP case study outputs and synthesis results: Sustainable livestock gui...
 
ADB ESLAP Case Study "Dairy value chain in Indonesia"
ADB ESLAP Case Study "Dairy value chain in Indonesia"ADB ESLAP Case Study "Dairy value chain in Indonesia"
ADB ESLAP Case Study "Dairy value chain in Indonesia"
 
Assessment of the environmental sustainability of plant-based meat and pork: ...
Assessment of the environmental sustainability of plant-based meat and pork: ...Assessment of the environmental sustainability of plant-based meat and pork: ...
Assessment of the environmental sustainability of plant-based meat and pork: ...
 
Case study on dairy value chain in China
Case study on dairy value chain in ChinaCase study on dairy value chain in China
Case study on dairy value chain in China
 
Global sustainable livestock investment overview
Global sustainable livestock investment overviewGlobal sustainable livestock investment overview
Global sustainable livestock investment overview
 
The impact of mechanization in smallholder rice production in Nigeria
The impact of mechanization in smallholder rice production in NigeriaThe impact of mechanization in smallholder rice production in Nigeria
The impact of mechanization in smallholder rice production in Nigeria
 
Biodiversity in agriculture for people and planet
Biodiversity in agriculture for people and planetBiodiversity in agriculture for people and planet
Biodiversity in agriculture for people and planet
 
Greenhouse gas (GHG) emissions & priority action in climate mitigation in the...
Greenhouse gas (GHG) emissions & priority action in climate mitigation in the...Greenhouse gas (GHG) emissions & priority action in climate mitigation in the...
Greenhouse gas (GHG) emissions & priority action in climate mitigation in the...
 
Evaluation of Rwanda climate services for agriculture through a gender lens
Evaluation of Rwanda climate services for agriculture through a gender lensEvaluation of Rwanda climate services for agriculture through a gender lens
Evaluation of Rwanda climate services for agriculture through a gender lens
 
Introduction to Climate-Smart Agriculture: Busia County, Kenya
Introduction to Climate-Smart Agriculture: Busia County, KenyaIntroduction to Climate-Smart Agriculture: Busia County, Kenya
Introduction to Climate-Smart Agriculture: Busia County, Kenya
 
Delivering information for national low-emission development strategies: acti...
Delivering information for national low-emission development strategies: acti...Delivering information for national low-emission development strategies: acti...
Delivering information for national low-emission development strategies: acti...
 
Delivering information for national low-emission development strategies: acti...
Delivering information for national low-emission development strategies: acti...Delivering information for national low-emission development strategies: acti...
Delivering information for national low-emission development strategies: acti...
 

Getting started with R

  • 1. Getting started with R Jacob van Etten
  • 2. What is R? “R is a free software environment for statistical computing and graphics.” www.r-project.org
  • 3. Why R? R takes time to learn and use. So why should I bother? There are more user-friendly programmes, right?
  • 4. 12 reasons to learn R 1. Rigour and strategy in data analysis – not “thinking after clicking”. 2. Automatizing repeated calculations can save time in the end. 3. A lot of stuff is simply not feasible in menu driven software.
  • 5. 12 reasons to learn R 4. R is not only about software, it is also an online community of user support and collaboration. 5. Scripts make it easy to communicate about your problem. Important for collaborative research! 6. Research becomes replicable when scripts are published.
  • 6. 12 reasons to learn R 7. R packages represent state-of-the-art in many academic fields. 8. Graphics in R are very good. 9. R stimulates learning – graduation from user to developer.
  • 7. 12 reasons to learn R 10. R is free, which saves you money. Or R may be the only option when budgets are restricted. 11. R encourages to freely explore new methods and learn about them. 12. Knowing to work with R is a valuable and transferable skill.
  • 8. It’s the long way but it’s worth it...
  • 9. Resources to learn R My two picks for you http://pj.freefaculty.org/R/Rtips.html
  • 10. Some R packages of interest RNCEP – Reanalysis data clim.pact– Downscaling climate data GhcnDaily – dailyweather data weatherData(R-Forge) – Daily weather data and derived bioclimatic variables relevant to plant growth raster – gives access to WorldClim data
  • 11. And a lot more here... http://cran.r-project.org/other-docs.html
  • 13. Choose a mirror nearby and then... Binaries “When downloading, a completely functional program without any installer is also often called program binary, or binaries (as opposed to the source code).” (Wikipedia)
  • 14. And finally,you can download R... When downloading has finished, run the installer
  • 15. The bare R interface
  • 16. RStudio makes life easier Rstudio.org
  • 17. Four parts Create a new R script: File – New – R Script Scripts, documentation Workspace/history Files, plots, packages and help Console
  • 18. Our first code... Type 1 + 1 into the script area. Then click “Run”. What happens?
  • 19. Exercises: running code Type a second line with another calculation (use “-”, “/”, or “*”) and click “Run” again. Select only one line with the mouse or Shift + arrows. Then click “Run”. Save your first code to a separate folder “Rexercises”.
  • 20. Following exercises In the next exercises, we will develop a script. Just copy every new line and add it to your script, without erasing the previous part. If you want to make a comment in your script, put a # before that line. Like this: #important to remember: use # to comment
  • 21. If the exercises are a bit silly... ...that’s because you are learning.
  • 22. Vector Type a new line with the expression 1:10 in the script and run this line. A concatenation of values is called a vector.
  • 23. Making a new variable If we send 1:10 into the console it will only print the outcome. To “store” this vector, we need to do the following. a <- 1:10 new variable “a” assign vector values 1 to 10
  • 24. Operations with vectors Try the following and see what happens. a a * 2 a * a b <- a * a b print(b)
  • 25. Other ways of making vectors d <- c(1, 6, 9) d class(d) f <- LETTERS f class(f) What is the difference between d and f?
  • 26. Functions Actually, we have already seen functions! Functions consist of a name followed by one or more arguments. Commas and brackets complete the expression. class(f) c(d,f) name argument
  • 27. Cheat sheet When you use R, you will become familiar with the most common functions. If you need a less common function, there are ways to discover the right one. For now, use the cheat sheet to look up the functions you need.
  • 28. Getting help on functions This will open help pages for the functions in your browser. ?c ?class Especially the examples are often helpful. Just copy and paste the code into the console and see with your own eyes what happens!
  • 29. Matrices We have already met the vector. If we put two or more vector together as columns, we get a matrix. X <- c(1,2,3) Y <- c(8,9,7) Z <- c(4,2,8) M <- cbind(X, Y, Z) How many columns and rows does M have?
  • 30. Data frames Matrices must consist of values of the same class. But often datasets consist of a mix of different types of variables (real numbers and groups). This is the job of data frames. L <- c(“a”, “b”, “c”) Df <- data.frame(X,Y,Z,L) Visualize Df like this: str(Df) What would happen if you tried to make a matrix out of these same vectors instead? Try and see.
  • 31. Getting data into R ?read.csv CSV files are a relatively trouble-free way of getting data into R. It is a fairly common format. You can make a CSV file in any spreadsheet software.
  • 32. Create a CSV file Add your own favorite actor, too. Open the file with Notepad. Make sure the values are separated by commas.
  • 33. Now use R to read it Now read it into R. actors <- read.csv(yourfile.csv) str(actors)
  • 34. Subsetting There are many ways of selecting only part of a data frame. Observe carefully what happens. actors[1:2,] actors[,1:2] actors[“Age”] actors[c(“Name”, “Age”)] subset(actors, Age> 40) Now create a new data frame with the actors younger than 45.
  • 35. Graphics The plot function makes graphs. plot(actors[c(“sex”, “Age”)])
  • 36. Summary You now know about: Variables Functions Vectors Matrices Data frames Getting tabular data into R Subsetting Simple plotting
  • 37. Time for your first fight...