---
title: "Manipulating data with dplyr"
toc-title: "Manipulating Data"
toc: true
format: html
execute:
  warning: false
  message: false
---

## Row Functions

### [arrange](https://r4ds.hadley.nz/data-transform#arrange)

Permute the ordering of the rows.  (Note the r in arrange, r for rows.)
 If you provide more than one column name, each additional column will be used to break ties in the values of preceding columns. 

```{r}
library(tidyverse)
library(nycflights13)
arrange(flights,month,dep_time)
```
 
 
### [distinct](https://r4ds.hadley.nz/data-transform#distinct)

Use `distinct` on several column names to find unique combinations.  The `.keep_all = TRUE` argument is used to retain all columsns

```{r}
library(nycflights13)
flights |> distinct(origin,dest)
```

## Column functions 

### [mutate](https://r4ds.hadley.nz/data-transform#sec-mutate)
Add new variables (columns), usually via a formula involving existing ones.

1. helper functions
  i) .before = 1
  ii) .after = some_var_name
  
### [select](https://r4ds.hadley.nz/data-transform#sec-select)
Useful if you have too many columns, choose which columns you wish to view.

1. tips & helper functions
  i) use : to select a range
  ii) use ! to exclude
  iii) use `where` with `is.factor()`, or `is.numeric()`, or `is.character()`
  iv) `starts_with("abc")`: matches names that begin with “`abc`”.
  v) `ends_with("xyz")`: matches names that end with “`xyz`”.
  vi) `contains("ijk")`: matches names that contain “`ijk`”.
  vii) `num_range("x", 1:3)`: matches `x1, x2` and `x3`.
  
### [rename](https://r4ds.hadley.nz/data-transform#rename)
Explicitly rename variables.  Do so in bulk with `janitor::clean_names`

### [relocate](https://r4ds.hadley.nz/data-transform#relocate)
Permute the ordering of columns (notice the c in relocate, c for columns)
