-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy path023_practice.Rmd
executable file
·67 lines (49 loc) · 3.27 KB
/
023_practice.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<style>@import url(style.css);</style>
[Introduction to Data Analysis](index.html "Course index")
# 2.3. Practice
<p class="info"><strong>Instructions:</strong> this week's exercise is called <code><a href="2_bmi.R">2_bmi.R</a></code>. Download or copy-paste that file into a new R script, then open it and run it with the working directory set as the <code>IDA</code> folder. If you download the script, make sure that your browser preserved its <code>.R</code> file extension.</p>
```{r run-exercise, include = FALSE, results = 'hide'}
source("code/2_bmi.R")
```
The exercise for this week is a quick overview of the main R object classes: vectors, matrices, data frames and factors. Each class will be useful to manipulate data in the next sessions, so make sure that you are familiar enough with them. The rest of this page documents the exercise and expands on what you learn from it.
## Computing the Body Mass Index
An important aspect of R syntax is *assignment*. As we saw previously, to put some values into an object, you need two things, separated by a right-left arrow (or by a single equal sign if you prefer). The example below creates an object that holds my Body Mass Index (BMI), and then evaluates if it sits in the "normal" range.
```{r my-bmi}
# Compute my Body Mass Index.
bmi <- round(703*134 / (70^2), 1)
# Create a text object (called a string).
assessment = "normal"
# Modify the assessment statement if BMI is below 18 or above 25.
if(bmi < 18) assessment = "below normal"
if(bmi > 25) assessment = "above normal"
cat("My BMI is approximately", bmi, ", which is", assessment)
```
In fact, it would make more sense to write a Body Mass Index *function*, which is not very difficult if you can survive the additional brackets. We will come back shortly to writing functions next week, but here's a quick example of a BMI function.
```{r bmi-function}
# A simple Body Mass Index function.
bmi <- function(weight, height, digits = 2) {
round(weight*703 / (height^2), digits)
}
# An object called 'bmi' now appears in your Workspace.
# Check result.
bmi
# This object is a function.
class(bmi)
# Example, with default argument of 2 digits.
bmi(weight = 134, height = 70)
# Another example, this time with no digits.
bmi(weight = 134, height = 70, digits = 0)
```
## Computing Quételet's BMI function
The exercise shows how to create the `bmi.quetelet` function that computes the BMI in its [original metric units](https://en.wikipedia.org/wiki/Body_mass_index), so that you can compute your own value.
```{r bmi-quetelet}
# Quételet's BMI function in kg/m.
bmi.quetelet
# My current BMI (AFAIK!) in kg/m.
bmi.quetelet(60, 1.75)
```
## Course reminders
Remember that a lot of what we cover here, especially with regards to the basic functioning of R, is also covered in many other tutorials. If you enjoy video tutorials, those by Anthony Damico are short and efficient: try his [2-minute approach to arithmetic in R][ajd-math].
[ajd-math]: http://www.screenr.com/hyT8 "How to do simple arithmetic in R (Anthony J. Damico)"
Most importantly, do not forget the textbook readings. Next week, we continue manipulating objects and delve a bit deeper into programming full-fledged functions: reading from textbooks will train you in performing these operations.
> __Next week__: [Functions](030_functions.html).