\(~\)
So far we’ve been relying upon R Scripts to store our work; However, R Studio supports several other file types, including R Markdown, a document type that allows for R code, that code’s output, and text written using the stylistic conventions of Markdown to coexist within the same document.
Recent installations of R Studio should come with R Markdown already available. You can check this by navigating:
File -> New File -> R Markdown
If you do not see “R Markdown” displayed in this drop down you’ll
need to install the rmarkdown
package:
# install.packages("rmarkdown")
library(rmarkdown)
At the top of an R Markdown document is the header:
After the end of the header you’ll see a code chunk:
After the setup code chunk you’ll see a section header:
Following the section header you’ll see ordinary text:
\(~\)
The purpose of R Markdown is to seamlessly blend R code, output, and
written text. This is accomplished by “knitting” your file into a
completed report. You can knit a file using the “Knit” button (blue yarn
ball icon), or (on windows) by pressing ctrl-shift-k
.
A few things to know about knitting:
install.packages()
and
View()
cannot be used in the environment where the document
is knit. You should comment-out or remove these commands before knitting
to prevent errors.As a reminder you should work on the lab with your assigned partner(s) using the principles of paired programming. Everyone in your lab group is responsible for the contents of the lab, but you should only submit a single document (created using R Markdown).
The simplest way to create a table from a categorical variable in
R
is using the table()
function. Below is an
example of a one-way frequency table of armed
variable in
the police
data set:
## Load police data
police <- read.csv("https://remiller1450.github.io/data/Police.csv")
## One-way table of the `gender` variable
table(police$armed)
##
## armed unarmed
## 7091 457
You should notice how we access the variable of interest using the
$
operator that was introduced in Lab #1.
The table function can accept any number of variables in your data
separated by commas. For example, we can create a two-way table of the
armed
and manner_of_death
variables via the
code below:
## Two-way table
table(police$armed, police$manner_of_death)
##
## shot shot and Tasered
## armed 6805 286
## unarmed 411 46
You should be aware that table()
creates a unique type
of object that can be stored and used as an input to other functions.
For example, the code below stores the previous two-way table and
transposes it using the t()
function:
my_table = table(police$armed, police$manner_of_death)
t(my_table)
##
## armed unarmed
## shot 6805 411
## shot and Tasered 286 46
Question #1: The data below, stored in the data
frame rtd
, contains data from an impaired driving
experiment where participants completed a baseline drive on a driving
simulator, consumed cannabis, then completed three additional simulator
drives while high. Participants were asked if they felt ready to drive
(on real roads, not in the simulator) before each drive, and the
responses are stored in the variables “RT1” (before the baseline)
through “RT4” (before the fourth/final drive). They were also asked if
they’d ever driven within two-hours of using cannabis (the variable:
“Ever”), and if they thought they were a better driver after using
cannabis (the variable: “Better”)
rtd <- read.csv("https://remiller1450.github.io/data/Ready.csv")
\(~\)
The prop.table()
function accepts a table as its input
and will convert the table’s frequencies to proportions:
my_table = table(police$armed, police$manner_of_death)
prop.table(my_table)
##
## shot shot and Tasered
## armed 0.90156333 0.03789083
## unarmed 0.05445151 0.00609433
By default, the function calculates proportions out of the total
number of cases contained in the table. However, the function can
calculate conditional proportions if the margin
argument is
used:
## Margin = 1 gives row proportions
prop.table(my_table, margin = 1)
##
## shot shot and Tasered
## armed 0.95966718 0.04033282
## unarmed 0.89934354 0.10065646
## Margin = 2 gives column proportions
prop.table(my_table, margin = 2)
##
## shot shot and Tasered
## armed 0.94304324 0.86144578
## unarmed 0.05695676 0.13855422
Question #2: For this question you should use the
rtd
data set introduced in Question #1. Begin by creating a
two-way frequency table where the rows represent values of the variable
Ever
and the columns represent values of the variable
RT2
, then use this table (and other R
commands
as necessary) to answer the following:
\(~\)
Table objects store their values in indexed positions similar to how
values are stored in a data frame. Thus, we could access the value in
position 1,1
of our example table using the following
code:
my_table = table(police$armed, police$manner_of_death)
my_table[1,1]
## [1] 6805
We will later learn about some built-in functions that calculate measures like odds ratios (and a whole lot of other output), but for now we can use index positions to calculate descriptive statistics like risk difference, relative risk, odds, and odds ratios by ourselves using indices.
Below are a few examples:
# Two-way frequency table
my_table = table(police$armed, police$manner_of_death)
# Row props
my_row_props = prop.table(my_table, margin = 1)
## Risk difference of being shot w/o first being tased for armed vs. unarmed
my_row_props[1,1] - my_row_props[2,1]
## Relative risk of being shot w/o first being tased for armed vs. unarmed
my_row_props[1,1]/my_row_props[2,1]
## Odds of being shot w/o first being tased for armed individuals
my_table[1,1]/my_table[1,2]
## Odds ratio of the likelihood of being shot w/o tasing for armed vs. unarmed
(my_table[1,1]*my_table[2,2])/(my_table[1,2]*my_table[2,1])
## [1] 0.06032364
## [1] 1.067075
## [1] 23.79371
## [1] 2.663043
Question #3: Similar to Questions #1 and #2, begin
by creating a two-way frequency table from the rtd
data
where the rows represent values of the variable Ever
and
the columns represent values of the variable RT2
, then use
this table (and other R
commands as necessary) to answer
the following:
\(~\)
Right click on the HTML file -> Send to -> Compressed folder