| title | R Style | ||
|---|---|---|---|
| editor_options |
|
In terms of the functions to use, attempt to use the functions and idioms from the tidyverse where at all possible
- ggplot2 for graphics
- tibbles instead of data frames
- dplyr and tidyr for data frame wrangling
- purrr for apply-type functionals
- forcats for factors
- lubridate for dates and times
- stringr for strings
- readr, haven, readxl or rio for reading from data formats
Use ggplot2, not base or lattice. The only exception is to use the
plot() method of an object when convenient. But do not create new graphics
using non-ggplot2 formats.
Some rules:
- do not use
setwd()in scripts EVER. - do not use
install.packagesin scripts. - Do not use
subsetortransform, usefilterandmutate - Do not use
aggregateorby, usesummarise - Do not use
merge, use the various dplyr*_joinfunctions - Do not use
orderorsort(when sorting data frames, it's okay for vectors), usearrange. - Do not use
uniqueorduplicatedwith data frames. Usedistinct() - Do not use
sapply,vapply,tapply, usepurrrmap functions. - It is okay to use
applyas the current purrr functions are annoying. However, they should only be used for matrices. Never on data frames. - use
haven::read_dtainstead offoreign::read.dta - use
readr::read.csvinstead ofread.csv - use
haven::read_spssintead offoreign::read.spss
It is okay to create new columns usine $<- or [[<-.
But do not filter for using logicals within [. Use filter().
In writing code, follow the tidyverse Style Guide.