RMardown example

This is an RMarkdown document. It is a plain-text file that mixes regular text with bits of R code (called “chunks”).

The regular text uses a format called markdown that lets you specify structure and formatting in plain text. You can make text bold or italicized (using underscores also works). lines that start with a “#” and a space in markdown are section headers. There are lots of other options (ordered lists, hyperlinks, images, etc); see https://www.markdownguide.org/cheat-sheet/ for a simple ‘cheat sheet’ or https://pandoc.org/MANUAL.html for a in-depth documentation on the type of markdown used in RStudio.

Setting up

Running some code

This script won’t do much, but let’s explore R’s built-in foxes dataset. The following code chunk loads the data and prints it. The code block starts and ends with three backticks (`). The text in the curly brackets at the top ({}) tells the interpreter that the code is R code and gives the chunk a label, which can be useful for debugging.

# first load the data
data(foxes)
# then print it
foxes
##     group avgfood groupsize area weight
## 1       1    0.37         2 1.09   5.02
## 2       1    0.37         2 1.09   2.84
## 3       2    0.53         2 2.05   5.33
## 4       2    0.53         2 2.05   6.07
## 5       3    0.49         2 2.12   5.85
## 6       3    0.49         2 2.12   3.25
## 7       4    0.45         2 1.29   4.53
## 8       4    0.45         2 1.29   4.09
## 9       5    0.74         3 3.78   6.13
## 10      5    0.74         3 3.78   5.59
## 11      5    0.74         3 3.78   5.83
## 12      6    0.57         3 2.24   3.93
## 13      6    0.57         3 2.24   4.34
## 14      6    0.57         3 2.24   6.09
## 15      7    0.74         3 2.75   6.85
## 16      7    0.74         3 2.75   6.05
## 17      7    0.74         3 2.75   3.21
## 18      8    0.42         3 1.88   3.72
## 19      8    0.42         3 1.88   3.92
## 20      8    0.42         3 1.88   5.71
## 21      9    0.68         3 3.77   5.95
## 22      9    0.68         3 3.77   4.66
## 23      9    0.68         3 3.77   6.22
## 24     10    0.65         3 1.73   6.70
## 25     10    0.65         3 1.73   6.22
## 26     10    0.65         3 1.73   1.92
## 27     11    0.51         3 2.21   4.31
## 28     11    0.51         3 2.21   3.43
## 29     11    0.51         3 2.21   5.97
## 30     12    0.98         7 3.84   5.30
## 31     12    0.98         7 3.84   3.88
## 32     12    0.98         7 3.84   3.18
## 33     12    0.98         7 3.84   2.78
## 34     12    0.98         7 3.84   2.99
## 35     12    0.98         7 3.84   3.58
## 36     12    0.98         7 3.84   6.86
## 37     13    0.60         4 3.02   4.24
## 38     13    0.60         4 3.02   3.46
## 39     13    0.60         4 3.02   4.19
## 40     13    0.60         4 3.02   2.35
## 41     14    0.77         4 3.43   4.72
## 42     14    0.77         4 3.43   4.42
## 43     14    0.77         4 3.43   4.83
## 44     14    0.77         4 3.43   6.02
## 45     15    0.73         4 2.65   5.83
## 46     15    0.73         4 2.65   5.02
## 47     15    0.73         4 2.65   5.37
## 48     15    0.73         4 2.65   2.01
## 49     16    0.72         4 3.54   4.25
## 50     16    0.72         4 3.54   4.38
## 51     16    0.72         4 3.54   4.84
## 52     16    0.72         4 3.54   3.18
## 53     17    0.66         4 2.45   4.63
## 54     17    0.66         4 2.45   3.86
## 55     17    0.66         4 2.45   4.82
## 56     17    0.66         4 2.45   6.54
## 57     18    1.21         8 5.07   3.29
## 58     18    1.21         8 5.07   3.99
## 59     18    1.21         8 5.07   3.84
## 60     18    1.21         8 5.07   3.98
## 61     18    1.21         8 5.07   4.44
## 62     18    1.21         8 5.07   4.74
## 63     18    1.21         8 5.07   3.70
## 64     18    1.21         8 5.07   4.75
## 65     19    0.68         4 2.59   5.21
## 66     19    0.68         4 2.59   5.78
## 67     19    0.68         4 2.59   4.50
## 68     19    0.68         4 2.59   3.28
## 69     20    0.78         4 3.13   3.68
## 70     20    0.78         4 3.13   3.72
## 71     20    0.78         4 3.13   4.29
## 72     20    0.78         4 3.13   3.58
## 73     21    0.78         4 3.56   3.75
## 74     21    0.78         4 3.56   2.67
## 75     21    0.78         4 3.56   5.43
## 76     21    0.78         4 3.56   6.31
## 77     22    0.80         4 3.35   5.79
## 78     22    0.80         4 3.35   4.64
## 79     22    0.80         4 3.35   5.34
## 80     22    0.80         4 3.35   7.55
## 81     23    0.69         4 3.16   4.77
## 82     23    0.69         4 3.16   3.48
## 83     23    0.69         4 3.16   4.33
## 84     23    0.69         4 3.16   5.03
## 85     24    0.71         4 3.04   4.09
## 86     24    0.71         4 3.04   4.11
## 87     24    0.71         4 3.04   5.47
## 88     24    0.71         4 3.04   6.25
## 89     25    1.03         5 3.66   4.75
## 90     25    1.03         5 3.66   5.32
## 91     25    1.03         5 3.66   5.39
## 92     25    1.03         5 3.66   3.84
## 93     25    1.03         5 3.66   3.15
## 94     26    0.78         5 3.92   5.17
## 95     26    0.78         5 3.92   4.42
## 96     26    0.78         5 3.92   4.65
## 97     26    0.78         5 3.92   3.80
## 98     26    0.78         5 3.92   3.41
## 99     27    0.79         5 2.89   4.12
## 100    27    0.79         5 2.89   3.28
## 101    27    0.79         5 2.89   3.93
## 102    27    0.79         5 2.89   2.96
## 103    27    0.79         5 2.89   2.55
## 104    28    0.91         6 4.54   5.00
## 105    28    0.91         6 4.54   4.36
## 106    28    0.91         6 4.54   7.10
## 107    28    0.91         6 4.54   5.84
## 108    28    0.91         6 4.54   5.68
## 109    28    0.91         6 4.54   2.32
## 110    29    0.67         4 2.75   5.00
## 111    29    0.67         4 2.75   3.81
## 112    29    0.67         4 2.75   4.81
## 113    29    0.67         4 2.75   3.94
## 114    30    0.41         3 1.91   3.16
## 115    30    0.41         3 1.91   2.78
## 116    30    0.41         3 1.91   3.86

What’s the average weight of the foxes? (the dataset doesn’t specify a unit)

mean(foxes$weight)
## [1] 4.529655

If the R code produces graphics, they will show up in the document. Let’s try with a simple histogram of fox weights:

hist(foxes$weight)

You can add more bins to the histogram too:

hist(foxes$weight, breaks = 10)