Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
r_workshop1 [2019/08/27 11:27]
clrccljuhasz [Indexing objects in R]
r_workshop1 [2021/10/13 16:02] (current)
lsherin
Line 1: Line 1:
 +<WRAP group>
 +<WRAP centeralign>​
 +<WRAP important>​
 +<wrap em> __MAJOR UPDATE__ </​wrap> ​
 +
 +<wrap em> As of Fall 2021, this wiki has been discontinued and is no longer being actively developed. </​wrap> ​
 +
 +<wrap em> All updated materials and announcements for the QCBS R Workshop Series are now housed on the [[https://​r.qcbs.ca/​workshops/​r-workshop-01/​|QCBS R Workshop website]]. Please update your bookmarks accordingly to avoid outdated material and/or broken links. </​wrap>​
 +
 +<wrap em> Thank you for your understanding,​ </​wrap>​
 +
 +<wrap em> Your QCBS R Workshop Coordinators. </​wrap>​
 +
 +</​WRAP>​
 +</​WRAP>​
 +<WRAP clear></​WRAP>​
 +
 ======= QCBS R Workshops ======= ======= QCBS R Workshops =======
  
Line 17: Line 34:
  
 Link to old [[http://​prezi.com/​ygatqgksbaso/​qcbs-r-workshop-1/​|Prezi presentation]] Link to old [[http://​prezi.com/​ygatqgksbaso/​qcbs-r-workshop-1/​|Prezi presentation]]
-===== Installing R and R Studio ===== 
  
-Before you can effectively start this workshop, you will need to install ​the R and R Studio softwares on your computer. For R, go to http://www.r-project.organd click on downloadYou will have to select a mirror site (usually one close to you) and select your platform (OS X, Windows or Linux)Download the file and launch it to complete the installation. You don't have to change the default settings.+Download ​the R [[https://github.com/​QCBSRworkshops/​workshop01/​blob/​dev/​workshop01-en/ReferenceScriptWorkshop1.R|script]] for this lesson 
 +===== Installing R and RStudio =====
  
-To install R Studio, go to http://www.rstudio.com/ and select R Studio from the Products tab. Click on the open source version of R Studio Desktop ​and select your platform ​to download itLaunch ​the file you just downloaded ​to complete the installation. ​Again, you can keep the default settings.+Before you can effectively start this workshop, you will need to install ​the R and RStudio softwares on your computer. For R, go to http://www.r-project.org/ and click on download. You will have to select a mirror site (usually one close to you) and select your platform ​(OS X, Windows or Linux)Download ​the file and launch it to complete the installation. ​You don't have to change ​the default settings.
  
-===== What is R? ===== +To install RStudio, go to http://​www.rstudio.com/​ and select RStudio from the Products tab. Click on the open source ​version of RStudio Desktop ​and select your platform to download it. Launch the file you just downloaded to complete the installation. Again, you can keep the default settings.
-R is an open-source ​programming language designed for statistical analysis, data analysis ​and data visualization.+
  
-===== Why use R? =====+===== Learning Objectives ​===== 
 +  - Understand what R and RStudio are; 
 +  - Use R as a calculator;​ 
 +  - Manipulate objects in R; 
 +  - Install, use an R package and get help.
  
-{{::​operating_systems.png?​50|}} ​ {{::​open_source.png?​100|}} {{::​the_why_r_plot_2.png?​200|}}+===== Understand what R and RStudio are =====
  
-R is open-source +==== What is R? ==== 
-This means that it is free, and constantly being updated and improved.+---- 
 +  * R is an open source ​programming language and environment. ​ 
 +  * It is designed for data analysisgraphical display ​and data simulations.  
 +  * It is one of the world'​s leading statistical programming environment.
  
 +==== Why use R? ====
 +----
  
-R is compatible ​with all major operating systems, so you can exchange ​work with people from all kinds of backgrounds, all over the world, using all kinds of different computer set ups.+  * R is **open-source**! This means that it is free, and constantly being updated and improved. 
 +  * It is **compatible**. R works on most existing ​operating systems
 +  * can help you create tables, produce graphs and do your statistics, all within ​the same program. So with Rthere is no need to use more than one program to manage data for your publications. Everything can happen in one single program. 
 +  * More and more scientists are using R every year. This means that its capacities are constantly growing and will continue to increase over time. This also means that there is a big online community ​of people that can help with any problems you have in R.
  
-R can help you create tables, produce graphs and do your statistics, all within the same programSo with R, there is no need to use more than one program to manage data for your publicationsEverything can happen in one single program.+{{::​operating_systems.png?​50|}} ​ {{::​open_source.png?100|}} {{::​the_why_r_plot_2.png?200|}}
  
-More and more scientists are using R every year. This means that its capacities are constantly growing and will continue to increase over time. This also means that there is a big online community of people that can help with any problems you have in R. +==== Using RStudio ​====
- +
-==== Using R Studio ​====+
 ----- -----
-R Studio ​is an integrated development environment (IDE) for R.  Basically, it's a place where you can easily use the R language, visualize tables and figures and even run all your statistical analyses. We recommend using it instead of the traditional command line as it provides great visual aid and a number of useful tools that you will learn more about over the course of this workshop. +RStudio ​is an integrated development environment (IDE) for R.  Basically, it's a place where you can easily use the R language, visualize tables and figures and even run all your statistical analyses. We recommend using it instead of the traditional command line as it provides great visual aid and a number of useful tools that you will learn more about over the course of this workshop.
-\\+
 ----- -----
 == CHALLENGE 1 == == CHALLENGE 1 ==
  
-Open R Studio+Open RStudio
  
-{{:​logo_rstudio.jpg?​200|R Studio ​logo}} +{{:​logo_rstudio.jpg?​200|RStudio ​logo}}
------ +
-\\+
  
 **Note for Windows users**: If the restriction "​unable to write on disk" appears when you try to open R-Studio, right-click on your R-Studio icon and chose:"​Execute as administrator"​ to open the program. **Note for Windows users**: If the restriction "​unable to write on disk" appears when you try to open R-Studio, right-click on your R-Studio icon and chose:"​Execute as administrator"​ to open the program.
 +----
 +**The console**
 +
 +When you open RStudio, the first thing that you see to the left of the screen is the "​console"​. This is where we will be working for the rest of this Introduction to R workshop. ​
 +
 +{{:​Rstudioconsole_fleche.png?​800|RStudio console}}
  
-When you open R studio, the first thing that you see to the left of the screen is the "​console"​. This is where we will be working for the rest of this Introduction to R workshop. ​Text in the console typically looks like this:+Text in the console typically looks like this:
 <file rsplus| Illustrating R console input and output> <file rsplus| Illustrating R console input and output>
 > output > output
Line 70: Line 99:
 </​file>​ </​file>​
  
-==== R as a calculator ===== +===== 2. Use R as a calculator ​====== 
------+
 The first thing to know about the R console is that you can use it as a calculator. The first thing to know about the R console is that you can use it as a calculator.
 <file rsplus| Addition>​ <file rsplus| Addition>​
Line 101: Line 130:
 == CHALLENGE 2 == == CHALLENGE 2 ==
  
-Complete ​the following skill testing question ​in the R Studio console: 2+16x24-56+Use R to calculate ​the following skill testing question: 2 + 16 x 24 - 56
  
 ++Challenge 2: Solution| \\ ''>​ 2 + 16 * 24 - 56\\ ++Challenge 2: Solution| \\ ''>​ 2 + 16 * 24 - 56\\
Line 108: Line 137:
 == CHALLENGE 3 == == CHALLENGE 3 ==
  
-Complete ​the following skill testing question ​in the R Studio console. +Use R to calculate ​the following skill testing question: \\ 
-Pay attention to the order of operations when thinking about this question+2 + 16 x 24 - 56 / (2 + 1) - 457 
-2+16x24-56/(2+1)-457+ 
 +//Pay attention to the order of operations when thinking about this question!//
  
 ++Challenge 3: Solution| \\ ''>​ 2 + 16 * 24 - 56 / (2 + 1) - 457\\ ++Challenge 3: Solution| \\ ''>​ 2 + 16 * 24 - 56 / (2 + 1) - 457\\
Line 132: Line 162:
 [1] 78.54''​\\ [1] 78.54''​\\
  
-R also has many built in constants like pi.\\+R also has many built-in constants like pi.\\
  
 ''>​ pi * 5^2\\ ''>​ pi * 5^2\\
Line 138: Line 168:
 ++ \\ ++ \\
  
-===== The concept of object ​in R =====+===== 3. Manipulate objects ​in R ===== 
 + 
 +==== Object ==== 
 +-----
  
 You have learned so far how to use R as a calculator to obtain various numerical values. However, it can get tiresome to always write the same code down in the R console, especially if you have to use some values repeatedly. This is where the concept of object becomes useful. You have learned so far how to use R as a calculator to obtain various numerical values. However, it can get tiresome to always write the same code down in the R console, especially if you have to use some values repeatedly. This is where the concept of object becomes useful.
  
-R is an object-oriented programming language. What this means is that we can allocate a name to values we've created to save them in our workspace. An object is composed of three parts: 1) a value we're interested in, 2) an identifier and 3) the assignment operator. The value can be almost anything we want: a number, the result of a calculation,​ a string of characters, a data frame, a plot or a function. The identifier is the name you assign to the value. Whenever you want to refer to this value, you simply type the identifier in the R console and R will return its value. Identifiers can include only letters, numbers, periods and underscores,​ and should always begin with a letter. The assignment operator resembles an arrow (''​%%<​-%%''​) and is used to link the value to the identifier. The following code clarifies these ideas:+R is an object-oriented programming language. What this means is that we can allocate a name to values we've created to save them in our workspace. An object is composed of three parts: 1) a value we're interested in, 2) an identifier and 3) the assignment operator. ​ 
 + 
 +  - The value can be almost anything we want: a number, the result of a calculation,​ a string of characters, a data frame, a plot or a function. ​ 
 +  - The identifier is the name you assign to the value. Whenever you want to refer to this value, you simply type the identifier in the R console and R will return its value. Identifiers can include only letters, numbers, periods and underscores,​ and should always begin with a letter. ​ 
 +  - The assignment operator resembles an arrow (''​%%<​-%%''​) and is used to link the value to the identifier. 
 + 
 +The following code clarifies these ideas:
  
 <file rsplus| Illustrating the concept of object> <file rsplus| Illustrating the concept of object>
Line 166: Line 205:
  
 </​file>​ </​file>​
-\\ +---- 
------ +=== Good practices in R code === 
-**R TIP**+ 
 +**Name** 
 +  * Try having short and explicit names for your variables. Naming a variable ''​var'' ​ is not very informative. 
 +  * Use an underscore (''​_''​),​ or a dot (''​.''​) to separate words within a name and try to be consistent! 
 +  * Avoid using names of existing functions and variables (e.g., ''​c'',​ ''​table'',​ ''​T'',​ etc.) 
 + 
 +**Space** 
 +  - Add spaces around all operators (''​='',​ ''​+'',​ ''​-'',​ ''<​-'',​ etc.) to make the code more readable. 
 +  - Always put a space after a comma, and never before (like in regular English).
  
-Try choosing explicit names for your objects. It is good practice and allows you to understand quickly what the object represents. Naming an object ''​variable''​ or ''​data''​ isn't very informative! 
 ----- -----
 == CHALLENGE 5 == == CHALLENGE 5 ==
Line 197: Line 243:
 ==== Types of data structures in R ==== ==== Types of data structures in R ====
 ----- -----
-Using R to analyse ​your data is an important aspect of this software. Data comes in different forms and can be grouped in distinct categories. Depending on the nature of the values enclosed inside your data or object, R classifies them accordingly. The following figure illustrates common objects found in R.+Using R to analyze ​your data is an important aspect of this software. Data comes in different forms and can be grouped in distinct categories. Depending on the nature of the values enclosed inside your data or object, R classifies them accordingly. The following figure illustrates common objects found in R.
  
 {{ :: imageobjr.png?​500 |Types of objects in R}} {{ :: imageobjr.png?​500 |Types of objects in R}}
  
-The first object is a vector. It is one of the most common objects in R. A vector is an entity consisting of a list of related values. All values in a vector must be the same mode. The main modes in R are numeric, character and logical. Numeric vectors are made of numbers only. Character vectors include text strings or a mix of text strings and other modes. You need to use ''​%%""​%%''​ to delimit elements in a character vector. Logical vectors include ''​TRUE/​FALSE''​ entries only. A vector with a single value (usually a constant) is called an atomic vector.+The first object is a **vector**. It is one of the most common objects in R. A vector is an entity consisting of a list of related values. All values in a vector must be the same mode. The main modes in R are **numeric****character** and **logical**. Numeric vectors are made of numbers only. Character vectors include text strings or a mix of text strings and other modes. You need to use ''​%%""​%%''​ to delimit elements in a character vector. Logical vectors include ''​TRUE/​FALSE''​ entries only. A vector with a single value (usually a constant) is called an atomic vector. 
 + 
 +Before we look at how to create different types of vectors, let's have a look at the generic method of creating vectors. If you recall what you have just learned, you will first have to identify some value you want to put in a vector and then link it to an identifier with the assignment operator (//i.e.// create an object). When you have more than one value in a vector, you need a way to tell R to group all these values to create a vector. The trick here is to use the ''​c()''​ function. Don't worry, you will learn about functions pretty soon in one of the following sections. For now, just remember to put your values between parentheses next to letter ''​c()''​ in this format: ''​vector.name %%<-%% c(value1, value2, value3, ...)''​. The function ''​c()''​ means combine or concatenate. It is a quick and easy function so remember it!
  
-Before we look at how to create different types of vectors, let's have a look at the generic method of creating vectors. If you recall what you have just learned, you will first have to identify some value you want to put in a vector and then link it to an identifier with the assignment operator (//i.e.// create an object). When you have more than one value in a vector, you need a way to tell R to group all these values to create a vector. The trick here is to use the ''​c''​ function. Don't worry, you will learn about functions pretty soon in one of the following sections. For now, just remember to put your values ​ between parentheses next to letter ''​c''​ in this format: ''​vector.name %%<-%% c(value1, value2, value3, ...)''​. The function ''​c()''​ means combine or concatenate. It is a quick and easy function so remember it! 
-\\ 
 Now that you know the generic method to create a vector in R, let's have a look at how to create different types of vectors. Now that you know the generic method to create a vector in R, let's have a look at how to create different types of vectors.
  
Line 221: Line 267:
 > bool_vector2 <- c(T, T, F) > bool_vector2 <- c(T, T, F)
 </​file>​ </​file>​
-\\+
 ----- -----
 == CHALLENGE 7 == == CHALLENGE 7 ==
Line 247: Line 293:
 </​code>​ </​code>​
  
-This demonstration might not be that convincing, but keep in mind that it can be very useful when you're manipulating data. These functions are really useful to provide a reproductible ​example for a question on stackoverflow for instance (see one more application in the part about data frames) !+This demonstration might not be that convincing, but keep in mind that it can be very useful when you're manipulating data. These functions are really useful to provide a reproducible ​example for a question on [[https://stackoverflow.com/|Stack Overflow]] ​for instance (see one more application in the part about data frames)!
 ----- -----
  
Line 268: Line 314:
  
 # Let's multiply x by itself. # Let's multiply x by itself.
-> x * x +> x * y 
-[1]  ​1 4 9 16 25+[1]  ​6 12 18 24 30
  
-# It is the same thing as using exponents: 
-> x^2 
-[1]  1 4 9 16 25 
 </​file>​ </​file>​
 +----
 +Another important type of object you will use regularly is the data frame. A data frame is a group of vectors of the same length (//i.e.// the same number of elements). Columns are always variables and rows are observations,​ cases, sites or replicates. Different modes can be saved in different columns (but always the same mode in a column). It is in this format that ecological data are usually stored. The following example shows a fictitious dataset representing 4 sites where soil pH and the number of plant species were recorded. There is also a "​Treatment"​ variable (fertilized or not). Let's have a look at the creation of a data frame.
  
-Another important type of object you will use regularly is the data frame. A data frame is a group of vectors of the same length (//i.e.// the same number of elements). Columns are always variables and rows are observations,​ cases, sites or replicates. Different modes can be saved in different columns (but always the same mode in a column). It is in this format that ecological data are usually stored. The following example shows a fictitious dataset representing 4 sites where soil pH and number of plant species were recorded. There is also a "​Treatment"​ variable (fertilised or not). Let's have a look at the creation of a data frame. +site_id ​soil_pH ​num_sp ​^ treatment ^
- +
-ID of the site soil pH # of species ​^ treatment ^+
 | A1.01 | 5.6 | 17 | Fertilised | | A1.01 | 5.6 | 17 | Fertilised |
 | A1.02 | 7.3 | 23 | Fertilised | | A1.02 | 7.3 | 23 | Fertilised |
Line 304: Line 347:
 <code rsplus> <code rsplus>
 > dput(my_df) > dput(my_df)
-structure(list(site_id ​= structure(1:​4,​ .Label ​= c("​A1.01",​ "​A1.02",​ "​B1.01",​ "​B1.02"​), class = "​factor"​), soil_pH = c(5.6, 7.3, 4.1, 6), num_sp = c(17, 23, 15, 7), treatment = structure(c(1L1L, 2L, 2L), .Label = c("​Fert",​ "​No_fert"​)class = "factor")), class = "​data.frame",​ row.names = c(NA, -4L))+structure(list(site_id = c("​A1.01",​ "​A1.02",​ "​B1.01",​ "​B1.02"​ 
 +), soil_pH = c(5.6, 7.3, 4.1, 6), num_sp = c(17, 23, 15, 7),  
 +treatment = c("​Fert"​, "​Fert",​ "​No_fert",​ "No_fert"​)), ​ 
 +class = "​data.frame",​ row.names = c(NA, -4L))
    
 # It's possible to rebuild the initial data frame (with some associated metadata as the class of variables) by copying and pasting the previous output: # It's possible to rebuild the initial data frame (with some associated metadata as the class of variables) by copying and pasting the previous output:
-> structure(list(site_id ​= structure(1:​4,​ .Label ​= c("​A1.01",​ "​A1.02",​ "​B1.01",​ "​B1.02"​), ​class = "​factor"​),​ +> structure(list(site_id = c("​A1.01",​ "​A1.02",​ "​B1.01",​ "​B1.02"​ 
-                 soil_pH = c(5.6, 7.3, 4.1, 6), +), soil_pH = c(5.6, 7.3, 4.1, 6), num_sp = c(17, 23, 15, 7),  
-                 num_sp = c(17, 23, 15, 7), +treatment = c("​Fert"​, "​Fert",​ "​No_fert",​ "No_fert"​)), ​
-                 ​treatment = structure(c(1L1L, 2L, 2L), .Label = c("​Fert",​ "​No_fert"​)class = "factor")),+
 class = "​data.frame",​ row.names = c(NA, -4L)) class = "​data.frame",​ row.names = c(NA, -4L))
 </​code>​ </​code>​
 ----- -----
-\\ 
 Other types of objects include matrices, arrays and lists. A matrix is similar to a data frame except that all cells in the matrix must be the same mode. An array is similar to a matrix but can have more than two dimensions. Arrays are usually used for advanced computation like numerical simulations and permutation tests. A list is an aggregation of various types of objects. For example, a list could include a vector, a data frame and a matrix in the same object. Other types of objects include matrices, arrays and lists. A matrix is similar to a data frame except that all cells in the matrix must be the same mode. An array is similar to a matrix but can have more than two dimensions. Arrays are usually used for advanced computation like numerical simulations and permutation tests. A list is an aggregation of various types of objects. For example, a list could include a vector, a data frame and a matrix in the same object.
-===== Indexing objects in R =====+==== Indexing objects in R ==== 
 +---- 
 +=== Indexing a vector ​===
  
 Typing an object'​s name in R returns the complete object. But what if our object is a huge data frame with millions of entries? It can easily become confusing to identify specific elements of an object. R allows us to extract only part of an object. This is called indexing. We specify the position of values we want to extract from an object with brackets ''​[ ]''​. The following code illustrates the concept of indexation with vectors. Typing an object'​s name in R returns the complete object. But what if our object is a huge data frame with millions of entries? It can easily become confusing to identify specific elements of an object. R allows us to extract only part of an object. This is called indexing. We specify the position of values we want to extract from an object with brackets ''​[ ]''​. The following code illustrates the concept of indexation with vectors.
Line 353: Line 399:
  
 </​file>​ </​file>​
-\\ 
 ----- -----
 == CHALLENGE 8 == == CHALLENGE 8 ==
Line 398: Line 443:
 ++++ ++++
 ----- -----
-\\ +=== Indexing a data frame === 
-For data frames, the concept of indexation is similar, but we usually have to specify two dimensions: the row and column numbers. The R syntax is\\ ''​dataframe[row number, column number]''​. ​\\Here are a few examples of data frame indexation. Note that the first four operations are also valid for indexing matrices.+For data frames, the concept of indexation is similar, but we usually have to specify two dimensions: the row and column numbers. The R syntax is 
 + 
 +''​dataframe[row number, column number]''​. ​ 
 + 
 +Here are a few examples of data frame indexation. Note that the first four operations are also valid for indexing matrices.
  
 <file rsplus| Indexing a data frame> <file rsplus| Indexing a data frame>
Line 413: Line 462:
  
 # Extract lines 2 to 4 # Extract lines 2 to 4
-> my_df[c(2:4), ]+> my_df[c(2,4), ]
  
 # Extract the "​site_id"​ column by referring directly to its name # Extract the "​site_id"​ column by referring directly to its name
Line 422: Line 471:
 > my_df[, c("​site_id","​soil_pH"​)] > my_df[, c("​site_id","​soil_pH"​)]
 </​file>​ </​file>​
 +----
 ==== A quick note on logical statements ==== ==== A quick note on logical statements ====
 +
 R gives you the possibility to test logical statements, //i.e.// to evaluate whether a statement is true or false. You can compare objects with the following logical operators: R gives you the possibility to test logical statements, //i.e.// to evaluate whether a statement is true or false. You can compare objects with the following logical operators:
  
Line 462: Line 512:
 [1] FALSE FALSE TRUE TRUE FALSE [1] FALSE FALSE TRUE TRUE FALSE
 </​file>​ </​file>​
 +\\
 +-----
 == CHALLENGE 10 == == CHALLENGE 10 ==
 > a) Extract the ''​num_sp''​ column from ''​my_df''​ and multiply its values by the first four values of the ''​num_vec''​ vector.\\ > a) Extract the ''​num_sp''​ column from ''​my_df''​ and multiply its values by the first four values of the ''​num_vec''​ vector.\\
Line 483: Line 535:
 </​code>​ </​code>​
 ++++ ++++
------ 
  
 ===== Functions ===== ===== Functions =====
 +----
 Most of the time with R, you will need to use functions to do what you want. Most of the time with R, you will need to use functions to do what you want.
  
Line 502: Line 553:
 ''//​function_name//​**(**arg1**,​** arg2**, ...)**''​ ''//​function_name//​**(**arg1**,​** arg2**, ...)**''​
  
-Ex:+Here an example:
 <file rsplus| Function syntax> <file rsplus| Function syntax>
 > sum(1, 2) > sum(1, 2)
 </​file>​ </​file>​
- 
  
 Arguments are **values** and the instructions the function needs to run. Arguments are **values** and the instructions the function needs to run.
Line 519: Line 569:
  
 On the last line, the output that appears is the **return value** of the function. In this case, it is the sum of ''​a''​ and ''​b'',​ 8.\\ On the last line, the output that appears is the **return value** of the function. In this case, it is the sum of ''​a''​ and ''​b'',​ 8.\\
-\\ 
  
 ----- -----
Line 559: Line 608:
 ++++ ++++
 ----- -----
-\\+=== Arguments ===
  
 Arguments each have a **name** that can be provided during a function call.\\ Arguments each have a **name** that can be provided during a function call.\\
Line 570: Line 619:
 log(8, base = 2) log(8, base = 2)
 </​file>  ​ </​file>  ​
-\\ 
- 
 ----- -----
 == CHALLENGE 12 == == CHALLENGE 12 ==
Line 605: Line 652:
 ++++ ++++
 ----- -----
-\\ 
  
 As a reference, here is a list of some of the most common R functions: As a reference, here is a list of some of the most common R functions:
Line 615: Line 661:
 help (or ?), help.search (or ??), help.start help (or ?), help.search (or ??), help.start
 </​code>​ </​code>​
-\\ 
- 
-==== Packages ==== 
  
 +===== 4. Install, use an R package and get help =====
  
 +==== Package ====
 +-----
 Packages are a grouping of functions and/or datasets that share a similar theme. Ex : statistics, spatial analysis, plotting... Packages are a grouping of functions and/or datasets that share a similar theme. Ex : statistics, spatial analysis, plotting...
  
Line 626: Line 672:
 They are usually available through the Comprehensive R Archive Network (CRAN)[[http://​cran.r-project.org/​web/​packages/​]] They are usually available through the Comprehensive R Archive Network (CRAN)[[http://​cran.r-project.org/​web/​packages/​]]
  
-Currently, more than 5877 package ​are publicly available.+Currently, more than 16000 packages ​are publicly available.
  
 To install packages on your computer, use the function ''​install.packages()''​ To install packages on your computer, use the function ''​install.packages()''​
Line 652: Line 698:
 </​code>​ </​code>​
  
-Now the function is found and the execution of our code leads to the following graph:\\ +Now the function is found and the execution of our code leads to the following graph: 
-{{::​qplot.png?​200|}}\\+ 
 +{{::​qplot.png?​200|}}
  
 It is good practice to unload packages once we are done with them because it might conflict with other packages. Unloading a package is done with the ''​detach()''​ function and by specifying that it is a package: It is good practice to unload packages once we are done with them because it might conflict with other packages. Unloading a package is done with the ''​detach()''​ function and by specifying that it is a package:
Line 659: Line 706:
 > detach(package:​ggplot2) > detach(package:​ggplot2)
 </​file>​ </​file>​
-===== Getting help and additional resources ===== 
  
-==== Getting help with functions ​====+==== Getting help ==== 
 +-----
  
 +=== Getting help with functions ===
  
 We've seen so far that R is really great and offers us a lot of functions to work with. Among all these functions, there are probably some that can do what we want. We've seen so far that R is really great and offers us a lot of functions to work with. Among all these functions, there are probably some that can do what we want.
Line 683: Line 731:
   * On the right, we have the description of the function   * On the right, we have the description of the function
  
-Usually, ​the functions have name that are representative of what they do. This makes it easier to use them. Remember this if you ever start to write your own functions!+Usually, functions have names that are representative of what they do. This makes it easier to use. Remember this if you ever start to write your own functions!
  
 Here the result that interests us is ''​base::​seq'',​ i.e. the function ''​seq''​ that can be found in the ''​base''​ package and that generates sequences.\\ Here the result that interests us is ''​base::​seq'',​ i.e. the function ''​seq''​ that can be found in the ''​base''​ package and that generates sequences.\\
-//Note//: the ''​base''​ package contains basic functions that load with R when you launch it and are therefore always available. +**Note**: the ''​base''​ package contains basic functions that load with R when you launch it and are therefore always available. 
 +----- 
 +=== Help page of functions ===
 We will use the ''​seq()''​ function to generate our sequence. But how does it work? What does it do? How should I use it? We will use the ''​seq()''​ function to generate our sequence. But how does it work? What does it do? How should I use it?
  
Line 713: Line 762:
   * **See Also**: Related functions that can sometimes be of use, especially when searching for the correct function for our needs.   * **See Also**: Related functions that can sometimes be of use, especially when searching for the correct function for our needs.
   * **Examples**:​ Some examples on how to use the function(s)   * **Examples**:​ Some examples on how to use the function(s)
- 
-\\ 
 ----- -----
 == Challenge 13 == == Challenge 13 ==
Line 747: Line 794:
 ++++ ++++
 ----- -----
-\\ +=== Getting help on the Web ===
- +
-==== Getting help on the Web ====+
  
 Usually, your best source of information will be your favorite search engine (Google, Bing, Yahoo, etc.) Usually, your best source of information will be your favorite search engine (Google, Bing, Yahoo, etc.)
Line 759: Line 804:
   * Learn to read discussion forums. Chances are other people already had the same problem and asked about it. Create your account on forums where questions about R are often asked like [[http://​stats.stackexchange.com|stackexchange]]. ​   * Learn to read discussion forums. Chances are other people already had the same problem and asked about it. Create your account on forums where questions about R are often asked like [[http://​stats.stackexchange.com|stackexchange]]. ​
   * Don't hesitate to search again with different keywords!   * Don't hesitate to search again with different keywords!
- 
-\\ 
  
 ----- -----
Line 777: Line 820:
 d) ''​ls''​ \\ d) ''​ls''​ \\
 ++++ ++++
------ +===== Additional resources =====
-\\ +
-==== Some useful books on R ==== +
-Dalgaard, P. - Introductory Statistics with R.\\ +
-Zuur, A.F., Ieno, E.N. & Meesters, E. - A Beginner'​s Guide to R.\\ +
-Crawley, M. - The R Book.\\ +
-Everitt, B.S. & Hothorn, T. - A Handbook of Statistical Analyses Using R.\\ +
-Kabacoff, R.I. - R in Action.+
  
-==== Some useful websites ​==== +=== Cheat Sheets ​===
-http://​stats.stackexchange.com/​ \\ +
-https://​www.zoology.ubc.ca/​~schluter/​R/​ \\ +
-http://​www.statmethods.net/​ \\ +
-http://​www.rseek.org/​ \\ +
-http://​www.cookbook-r.com/​ \\ +
-http://​cran.r-project.org/​doc/​contrib/​Baggott-refcard-v2.pdf+
  
-==== R script reference ==== +[[https://​www.rstudio.com/​resources/​cheatsheets/​|https://​www.rstudio.com/​resources/​cheatsheets/​]]
  
 +These cheat sheets are also directly available in RStudio (Help -> Cheat Sheets)
 +----
 +
 +=== A few books ===
 +
 +  * R for Dummies
 +  * A Handbook of Statistical Analyses using R
 +  * R in Action
 +  * Introductory Statistics in R
 +  * A Beginner'​s Guide to R
 +  * The R Book
 +
 +----
 +
 +=== A few useful links ===
 +
 +  * [[http://​r4ds.had.co.nz/​index.html|http://​r4ds.had.co.nz/​index.html]]
 +  * [[https://​cran.r-project.org/​doc/​manuals/​r-release/​R-intro.html|https://​cran.r-project.org/​doc/​manuals/​r-release/​R-intro.html]]
 +  * [[http://​cran.r-project.org/​doc/​contrib/​Baggott-refcard-v2.pdf|http://​cran.r-project.org/​doc/​contrib/​Baggott-refcard-v2.pdf]]
 +  * [[http://​statmethods.net/​|http://​statmethods.net/​]]
 +  * [[https://​support.rstudio.com/​hc/​en-us/​categories/​200035113-Documentation|https://​support.rstudio.com/​hc/​en-us/​categories/​200035113-Documentation]]
 +  * [[http://​cookbook-r.com/​|http://​cookbook-r.com/​]]
 +
 +==== R script ==== 
 +-----
 Want to revise/​practice the material seen here at home? Want to revise/​practice the material seen here at home?
  
-[[{}{ :referencescriptworkshop1.r }|Download R script]]+[[{}{ wiki:workshop01-en.r }|Download R script]]