Getting started with R

1,094 views

Published on

Presentation by Jacob van Etten.

CCAFS workshop titled "Using Climate Scenarios and Analogues for Designing Adaptation Strategies in Agriculture," 19-23 September in Kathmandu, Nepal.

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,094
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
25
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Getting started with R

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

×