Programming Cheatsheets

Cheatsheet for R

Please contribute additional tasks and solutions.

Read/write files
File format Package Function(s) Tutorial
xls(x) readxl read_excel() Overview
Transforming data frames
Task Package Function(s) Tutorial
Wide-to-long tidyr gather(), pivot_longer()
Long-to-wide tidyr spread(), pivot_wider()
Summarize columns dplyr summarize(), count()
Order data frame base order()
dplyr arrange()
Summarize data frame skimr skim()
base str()
Merge data frames dplyr bind_cols(), bind_rows(), left_join(), right_join() + other *_join()s
Select rows base df[..., ]
dplyr filter()
Select columns base df[, ...]
dplyr select()
Model functions
Task Package Function(s) Tutorial
Linear mixed effects lme4 lmer(), glmer()
Bayesian modeling brms brm()
Support-vector machines e1071 svm()
parsnip svm_poly(), svm_rbf()
Naïve Bayes e1071 naiveBayes()
parsnip/discrim naive_Bayes()
Random Forest randomForest randomForest()
parsnip rand_forest()
Neural network keras Framework
tensorflow Framework
nnet nnet(),multinom() (note: single-hidden-layer only)
GAMLSS gamlss gamlss()
LQM(M) lqmm lqm(), lqmm()
Nearest Neighbour parsnip nearest_neighbor()
Model evaluation
Task Package Function(s) Tutorial
Evaluate predictions cvms evaluate() - regression and classification. Also: ID-level evaluation. ID evaluation
yardstick TODO
Create confusion matrix cvms evaluate() or confusion_matrix() - see also plot_confusion_matrix() Create confusion matrix
yardstick conf_mat()
caret confusionMatrix()
Cross-validation cvms + groupdata2 cross_validate() for lm()/lmer()/glm()/glmer() or cross_validate_fn() for most others Cross-validate custom model functions
caret trainControl? TODO
tidymodels TODO
Plotting with ggplot2
Task Package Function(s) Tutorial
Combine ggplots patchwork +, /, ` `
Add images ggimage geom_image(), geom_bgimage(), geom_icon()
Add pokemons ggimage geom_pokemon()
Plot confusion matrix cvms plot_confusion_matrix() Create confusion matrix
Sharing code examples
Task Package Function(s) Tutorial
Create reproducible example reprex reprex(), reprex:::prex_rtf() (dev version) Use prex_rtf()

Cheatsheet for Python