@@ -56,6 +56,11 @@ The objectives of this session will be to:
- Manage a workspace in R
- Call functions
- Manage packages
- Be ready to write graphics !
<center>
{width=400px}
</center>
### Acknowledgments
...
...
@@ -74,6 +79,10 @@ The objectives of this session will be to:
http://swcarpentry.github.io/r-novice-gapminder/
</div>
\
- **Margot** and **Alexandre** for assistance!
# Some R background
...
...
@@ -170,7 +179,6 @@ It is waiting for the next command. Write just `100` :
```{r calculatorstep5, include=TRUE}
(3 + (5 * (2 ^ 2))) # hard to read
3 + 5 * 2 ^ 2 # clear, if you remember the rules
3 + 5 * (2 ^ 2) # if you forget some rules, this might help
```
\
...
...
@@ -218,7 +226,7 @@ factorial(9)
### Comparing things
Comparisons can be made with R. The result will return a `TRUE` or `FALSE` value.
Comparisons can be made with R. The result will return a `TRUE` or `FALSE` value (`boolean` type).
equality (note two equal signs read as "is equal to")
...
...
@@ -323,6 +331,17 @@ a
\
```R
x + z
```
\
How to test the type of the variable?
```{r VandAstep20, include=TRUE}
is.character(z)
b <- 1/40
b
typeof(b)
```
### Variables names
...
...
@@ -348,11 +367,11 @@ What you use is up to you, but be consistent.
```
min_height
max.height
_age
_age # no
.mass
MaxLength
min-length
2widths
min-length # no
2widths # no
celsius2kelvin
```
\
...
...
@@ -396,107 +415,82 @@ This block allows you to view the different outputs (?help, graphs, etc.).
\
Test that your `logarithm` function can work in base 10
### A code editor
```{r VandAstep10, include=TRUE}
10^logarithm(12, base = 10)
```

\
RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can
We can also define our own function with
```
function_name <- function(a, b){
result_1 <- operation1(a, b)
result_2 <- operation2(result_1, b)
return(result_2)
}
```
- click on the `Run button` above the editor panel, or
- select “Run Lines” from the “Code” menu, or
- hit `Ctrl`+`Return` in Windows or Linux or `Cmd`+`Return` on OS X. To run a block of code, select it and then Run.
\
<div id="pquestion">Write a function to test the base of the logarithm function</div>
If you have modified a line of code within a block of code you have just run, there is no need to reselect the section and Run, you can use the next button along, Rerun the previous region. This will run the previous code block including the modifications you have made.
<div id="pquestion">How to write a function to test if a number is even?</div>
\
RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can
- click on the `Run button` above the editor panel, or
- select “Run Lines” from the “Code” menu, or
- hit `Ctrl`+`Return` in Windows or Linux or `Cmd`+`Return` on OS X. To run a block of code, select it and then Run.
```{r VandAstep11, include=TRUE}
even_test <- function(x){
modulo_result <- x %% 2 # %% is modulo operator
is_even <- modulo_result == 0
return(is_even)
}
If you have modified a line of code within a block of code you have just run, there is no need to reselect the section and Run, you can use the next button along, Rerun the previous region. This will run the previous code block including the modifications you have made.
even_test(4)
even_test(3)
```
Copy your `logarithm` and `base_test` into a `tp_1.R` file
**Note :** A function can write in several forms.
\
We can now clean your environment
No We can now clean your environment
```{r VandAstep15, include=TRUE}
rm(x)
...
...
@@ -555,12 +549,12 @@ c(1:5)
A mathematical calculation can be performed on the elements of the vector:
```{r Vecstep3, include=TRUE}
2^(1:5)
2^c(1:5)
```
```{r Vecstep4, include=TRUE}
x <- 1:5
x <- c(1:5)
2^x
```
...
...
@@ -590,13 +584,9 @@ is.vector(x)
```{r Vecstep8, include=TRUE}
y <- c(a = 1, b = 2, c = 3, d = 4, e = 5)
typeof(y)
is.vector(y)
```
\
\
We can compare the elements of two vectors:
```{r Vecstep9, include=TRUE}
...
...
@@ -606,21 +596,12 @@ x == y
```
\
\
If you use `all.equal` :
```{r Vecstep10, include=TRUE}
all.equal(x, y)
isTRUE(all.equal(x, y))
isTRUE(all.equal(x, y, check.attributes = FALSE))
```
\
<fieldset id='pencadre' style='text-align: left'>
<legend style='border: 0px;'>Summary box</legend>
<li> A variable can be of different types : `numeric`, `character`, `vector`, etc.</li>
<li> A variable can be of different types : `numeric`, `character`, `vector`, `function`, etc.</li>
<li> Calculations and comparisons apply to vectors.</li>
<li> Do not hesitate to use the help box to understand functions! </li>