-
Notifications
You must be signed in to change notification settings - Fork 1
/
tidyverse-forget.Rmd
52 lines (44 loc) · 1.95 KB
/
tidyverse-forget.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---
title: "Tidyverse functions that I always forget"
author: "Jessica Minnier"
date: '`r Sys.Date()`'
output:
github_document:
toc: yes
---
<!-- The .md file is generated from a .Rmd file, please edit the .Rmd file! -->
```{r setup, include=FALSE}
library(tidyverse)
library(readxl)
library(skimr)
library(janitor)
library(jmmisc)
#### Global chunk options -----------------------------
knitr::opts_chunk$set(
eval = TRUE, # whether to run code in code chunk
include = TRUE, # whether to include the chunk output
echo = TRUE, # Whether to show code chunk in final output
error = TRUE, # whether to display error messages
message = FALSE, # whether to preserve messages
warning = FALSE, # whether to preserve warnings
comment = "#>", # a character string to append at start
# of each line of results in final document
tidy = FALSE, # whether to tidy code chunks for display
dpi = 96,
fig.width = 6, # consistent width for figures
fig.asp = 0.618, # the golden ratio, can be adjusted in individual chunks
out.width = "100%", # controls the output size
fig.align = "center" # give plot room to breathe
)
```
- [`tibble::enframe()`](https://tibble.tidyverse.org/reference/enframe.html) converts a named vector to a tibble and `tibble::deframe()` does the opposite
- [`forcats::fct_infreq()`](https://forcats.tidyverse.org/reference/fct_inorder.html) reorders a character or factor column in place, and can be used in ggplot:
```{r}
ggplot(mtcars %>% mutate(carb = as.character(carb)), aes(fct_infreq(carb),fill=carb)) + geom_bar()
```
- [`stringr::str_detect`](https://stringr.tidyverse.org/reference/str_detect.html) can be used to search for multiple strings in a vector by using `paste` collapse:
```{r}
fruit <- c("apple", "banana", "pear", "pinapple")
mystrings <- c("apple","pear","grape")
str_detect(fruit, paste(mystrings,collapse="|"))
```