library(knitr)
opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
R Markdown
FilesThis is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
R
code chunks within the document. You can embed an R code chunk like this:summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
echo
and eval
commands in the opening line. Insert a few code chunks below using the insert
tab at the top of this window. Then, change the echo
and eval
arguments to TRUE
or FALSE
and see how different combinations of these arguments change the output when you knit. I have done the first one for you. Notice too that each R
code chunk requires a unique title argument (here ‘cars variant 1’), or the Rmd will not knit.summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
echo
and eval
do, based on your manipulations?-
and a space, then type your answer (as I did here). This will indent your answer in RMarkdown (knit
to visualize the difference). Use this format throughout the doc to format your answers.echo
and eval
, based on your manipulations?Getting more familiar with RMarkdown
knitr
package has lots of options explained
config
chunk. That way future users will know exactly what they need to install.library(scales)
library(knitr)
opts_chunk$set(background = "gray80", tidy = FALSE, cache = FALSE,
comment = "", dpi = 72, fig.path = "RMDfigs/", fig.width = 4,
fig.height = 4)
x
value is just numbers 1-100 for an x axis value. This might be time or distance, etc.rnorm
function, and then add a trend with the seq
function.letters
.# setwd('~/Desktop')
x <- 1:100
y <- rnorm(100, sd=3) + seq(10.05, 20, 10/100)
z <- factor(rep(letters[1:5], each=20))
dat <- data.frame(x, y, z)
knitr
head(dat)
x y z
1 1 10.044521 a
2 2 5.051540 a
3 3 9.019966 a
4 4 10.422187 a
5 5 11.500110 a
6 6 8.175666 a
knitr
package has a simple built-in function for dealing with tables. This works well in either html or pdf output.kable(head(dat))
x | y | z |
---|---|---|
1 | 10.044521 | a |
2 | 5.051540 | a |
3 | 9.019966 | a |
4 | 10.422187 | a |
5 | 11.500110 | a |
6 | 8.175667 | a |
knitr
and RMarkdown generally, is the ability to embed real R commands in sentences, so that you can report actual values instead of constantly copying and pasting when results change a little bit.R
Insert a code chunk below and complete the following tasks:
R
follows the normal priority of mathematical evaluationR
Insert a code chunk below and complete the following tasks:
concatenate
function to print a sentenceInsert a code chunk below and complete the following tasks:
c
functionc
functionvec1<-c("I", "am", "great","at","R")
fac1<-as.factor(vec1)
print(fac1)
[1] I am great at R
Levels: am at great I R
str
and class
to evaluate your variablesInsert a code chunk below and complete the following tasks:
mean
, sd
, sum
, length
, and var
log
and sqrt
functions on your vectorComplete the following tasks in the codechunk below: - Note: If you ever want someone else to be able to perfectly reproduce your results, always set the random seed at the top. Any number will do. Note that it never hurts to set the seed, but robust results should always stand up to random number generators.
seq
function and calculate two basic statistics on your vectorsample
it with equal probabilityrnorm
function then sample
that distribution with and without replacementhist
to plot your normally distributed variableset.seed(1415)
You can also embed plots in your pdf document (knit
to view), for example:
echo = FALSE
parameter was added to the code chunk to prevent printing of the R code that generated the plot.{}
) section.Insert a code chunk below and complete the following tasks, make sure to label all plot axes and have fun with colors!
seq
and make two different plots by changing the type
argumentrnorm
and make two different plots using hist
by varying the breaks
argument (what does breaks
appear to do?)par()
arguments to create a composite figure of the above graphs.Insert a code chunk below and complete the following tasks:
data.frame
rownames
and c
str
mean
of each numeric variablehead
and tail
on your dataframe?By opening this .Rmd file, you have automatically set your working directory to the folder containing it. Now, you can access data from this directory or a sub-directory in this folder. You can do this by including that part of the path in the read.csv
function. Insert a code chunk below and complete the following tasks:
read.csv
to read your file instr
and head
to view your data structure$
and [ ]
operators to select out different parts of the dataframe.$
.tapply
function to calculate the mean
and var
of temp by habitat type and temp by elevation.