You can put chunks of R code by putting three backticks at the beginning and end of the chunk. You need to give each chunk a unique name in the {}

Echo

Here is an example with echo=FALSE

## [1] 0.8810438

Here is an example with echo=TRUE

x <- rnorm(100)
y <- 2*x+rnorm(100)
cor(x,y)
## [1] 0.8915812

and here is an example with echo being default(true)

x <- rnorm(100)
y <- 2*x+rnorm(100)
cor(x,y)
## [1] 0.8955672

Other stuff

TO insert r code inline, you use a single beginning and end backtick. An example may look like this ‘r cor(x,y)’. where the value of the correlation is reported. You may type in italicized text like this, and bold text like this. For those of you who know Latex, you can type equations using the usual inline dollar command sign, something like this \(\frac{x}{\sin{(x*\pi)}}\).

Graphics

lets do a simple plot

Good Practice

In general you want the code in a markdown document to be self contained. That is anyone should be able to run the commands in your document and get the same results you do. Hence, all datawrangling and evaluations should be done in R(not using Excel or other programs on the side)

When reading in datafiles, it is best to have these links from a URL. I find that storing files on Github works the best for those that I create/host (be sure to link to the “raw” version of the file).

# Draft Kings Salaries of Players on Houston Rockets and LA Lakers
dat <- read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/DraftKings_Rockets_Lakers.csv")
head(dat)
##   Position                    Name...ID              Name       ID
## 1    PG/SG      James Harden (15014112)      James Harden 15014112
## 2    PG/SF      LeBron James (15014113)      LeBron James 15014113
## 3     PF/C     Anthony Davis (15014114)     Anthony Davis 15014114
## 4       PG Russell Westbrook (15014115) Russell Westbrook 15014115
## 5    SF/PF  Robert Covington (15014116)  Robert Covington 15014116
## 6    PG/SG      James Harden (15014076)      James Harden 15014076
##   Roster.Position Salary                     Game.Info TeamAbbrev
## 1             CPT  19200 LAL@HOU 08/06/2020 09:00PM ET        HOU
## 2             CPT  18600 LAL@HOU 08/06/2020 09:00PM ET        LAL
## 3             CPT  17400 LAL@HOU 08/06/2020 09:00PM ET        LAL
## 4             CPT  15600 LAL@HOU 08/06/2020 09:00PM ET        HOU
## 5             CPT  13500 LAL@HOU 08/06/2020 09:00PM ET        HOU
## 6            UTIL  12800 LAL@HOU 08/06/2020 09:00PM ET        HOU
##   AvgPointsPerGame
## 1            59.34
## 2            55.09
## 3            50.72
## 4            51.19
## 5            29.06
## 6            59.34

The average player salary is 4736 dollars.

Display code without Evaluating

Sometimes, you will want to put your code in document without evaluating. This may be done by setting the option eval=FALSE

# All R code used in document
x <- rnorm(100)
y <- 2*x+rnorm(100)
cor(x,y)
plot(x,y, main="Scatterplot of X and Y")
dat <- read.csv("https://raw.githubusercontent.com/tmatis12/datafiles/main/DraftKings_Rockets_Lakers.csv")
head(dat)