You put chunk of code by putting three ticks at the beginning and end of the chunck. Each chunk must have its unique name in the {} and always include r after the first {, it is to specify the language of code being evaluated, could be python or sql…
The eco command tell R wether or not to print the code that generated the result. Setting echo=TRUE will make R print the code inserted. Set echo=FALSE when we don’t want to print the code.
Here is an example with echo=False
## [1] 0.8951083
Here is an example with echo=TRUE
x<-rnorm(100)
y<-2*x+rnorm(100)
cor(x,y)
## [1] 0.8962805
Leaving echo in the default setting is the same as echo=TRUE
x<-rnorm(100)
y<-2*x+rnorm(100)
cor(x,y)
## [1] 0.9046264
To insert inline, you use asingle b3ginning and end backtick like this 0.9046264, where the value of the correlation is reported. You may type in italic text like this and bold text like this. For those of you with latex , you can type equations inline dollar command: \(\frac{x}{\sin{(x*\pi)}}\).
Lets do a simple plot
plot(x,y,main="Scatterplot of X and Y")
In general you want the code in a markdown to be self conained. 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 linked from a URL. I find that storing files on GitHub works the best. ( Be sure to link the ‘raw’ version of the file).
Good pratice to store data on github and provide link to access it.
#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(hat)
##
## 1 function (x, intercept = TRUE)
## 2 {
## 3 if (is.qr(x))
## 4 n <- nrow(x$qr)
## 5 else {
## 6 if (intercept)
The head(hat) will just preview the first six lines of the data
The average player salary is 4736 dollars.
We can display display the code without evaluting offering the possibilty of being copied by someone else
#ALL R code used in the 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(hat)
Once completed and satisfied , Publish your work to an Html link