Session 5

2023-10-18

The pipe operator

  • It can be written as %>% or |>

  • Makes your code more clear

mutate(x, y=x+1)
## same as 
x %>% 
  mutate(y=x+1)
## or
x |>
  mutate(y=x+1)
  • Use a keyboard shortcut to put it in a script!

  • In my mac is Ctrl+Shift m ## Why?

  • With the pipe operator we can chain multiple functions together.

  • Calculate mean total points grouped by playoffs.

nba <- readRDS('./nba.rds')

nba_grouped <- group_by(nba, Playoff) 

nba_summary <- summarize(nba_grouped,
                         avg_pts=mean(PTS))
nba_summary <- readRDS('./nba.rds') %>% 
  group_by(Playoff) %>% 
  summarize(avg_pts=mean(PTS))

Rmarkdown

  • File format for reports.

  • It combines R code and text.

  • It generates documents (pdf, word, html).

Create a file inside your project

Contents

  • Line 11 is text (level 2 header).

  • Line 15 is regular text.

  • Lines 17-19 are R code

Rmarkdown

  • R code chunks appear between 3 backticks (`).

  • When we run the file, R runs all the code inside this chunks and puts the output in the final document.

  • Ctrl+Shift+i inserts a code chunk.

  • Outside the tags, your can use markdown

Knit

  • Press the knit button to generate the final document (pdf, word or html file).