15:00
~30 min
for
loops and if
conditional executionsAtomic vectors are homogeneous/flat objects, i.e. all the objects composing the sequence must be of the same type, and cannot contain other (nested) sequences.
Four main types (?typeof
) of atomic vectors:
Character | ?is.character |
Other structure in R are based on atomic vectors, i.e. are of one of the base types but have more structure (similar structures are called ?class
)
Factors are discrete (i.e. based on ?integer
s) variables with labels.
Factors | ?is.factor |
[1] male female female
Levels: female male other
Tip
You can investigate the internal structure of any R objects using ?str
.
Other structure in R are based on atomic vectors, i.e. are of one of the base types but have more structure (similar structures are called ?class
)
Dates are counts (based on ?double
s) of days since 1970-01-01.1
Dates | ?as.Date |
Date-Time are counts (based on ?double
s) of seconds since 1970-01-01.
List vectors are heterogeneous/nestable objects, i.e. objects composing the sequence can be of distinct types, and can contain other (nested) sequences.
List | ?list |
$age
[1] 70 85 69
$height
[1] 1.50 1.72 1.81
$at_risk
[1] TRUE FALSE TRUE
$gender
[1] male female female
Levels: female male other
Other structure in R are based on list vectors, i.e. are heterogeneous sequence of objects.
data frames are ordered list of equally sized homogeneous named vectors. I.e. the are used for tabular data:
?names
)?length
, i.e. number of rows (?nrow
)Data frames | ?data.frame |
Tip
During the course we will see, explain and use tibbles (from the package {tibble}
): a modern, enhanced, better displayed, and with stricter and more consistent structure than standard data frames.
Important
R works on vectors of two types only:
Think of objects in R (any objects in R!) as a train (either atomic or list) made of wagons:
Important
You can refer to subsetting objects (i.e., a train) as performing two operations mainly:
You can select more than one object/wagon when subsetting, but a single one only when extracting!
Three ways to identify elements (i.e., wagons):
Important
A data frame (and tibble
s) can be see as a “matrix” (or a table).
Data frames values can be subsetted using “[rows, column]” notation
multiple selection | [ |
Tip
Use the additional argument drop = FALSE
to maintain the data frame structure. Using tibble
s we will consistently get always a tibble
when subsetting with coordinates!
Important
You can refer to subsetting objects (i.e., a train) as performing two operations mainly:
You can select more than one object/wagon when subsetting, but a single one only when extracting!
Two ways to identify a (single!) element (i.e., a wagon):
Your turn
Connect to our pad(https://bit.ly/ubep-rws-pad-ed3)
Connect to the Day-2 project in RStudio cloud (https://bit.ly/ubep-rws-rstudio)
Before to evaluate it, in the pad, under the section 2.2. Ex7
, write (in a new line) what is your expected result (including an error).
Before to evaluate it, in the pad, under the section 2.2. Ex8
, write (in a new line) what is your expected result (including an error).
Then, open the script 04-atomic-vectors.R
and follow the instruction step by step.
Then, open the script 05-subsetting.R
and follow the instruction step by step.
15:00
Important
Coercion rule from specific to general:logical > integer > double > character
Subset operation can be performed in sequence on the same object directly.
Crucial to know if you are working on a subset of an object or its content.
YOU: Connect to our pad(https://bit.ly/ubep-rws-pad-ed3) and write there questions & doubts (and if I am too slow or too fast)
ME: Connect to the Day-2 project in RStudio cloud
02-vectors.R
03-matrices_dataframes.R
Tip
You don’t need to test if a logical is TRUE
or FALSE
, they are already TRUE
or FALSE
!
sum_one <- function(x) {
1 x + 1
}
sum_one(x = 3)
[1] 4
Your turn
Connect to our pad(https://bit.ly/ubep-rws-pad-ed3)
Connect to the Day-2 project in RStudio cloud (https://bit.ly/ubep-rws-rstudio)
2.2. Ex9
, write (in a new line) what is your expected result from the following computation:06-cond-and-funs.R
and follow the instruction step by step.20:00
Important
?if
returns the last computed value, if any, or NULL
otherwise (which may happen if there is no else
).?for
returns NULL
YOU: Connect to our pad(https://bit.ly/ubep-rws-pad-ed3) and write there questions & doubts (and if I am too slow or too fast)
ME: Connect to the Day-2 project in RStudio cloud (https://bit.ly/ubep-rws-rstudio): script 04-cond_loop.R
10:00
To create the current lesson we explored, use, and adapt contents from the following resources:
The slides are made using Posit’s Quarto open-source scientific and technical publishing system powered in R by Yihui Xie’s kintr.
This work by Corrado Lanera, Ileana Baldi, and Dario Gregori is licensed under CC BY 4.0
UBEP’s R training for supervisors