Everyday-R: Practical R for Data Science
Chapter 1 Introduction
Note: this book is a work in progress. All source code for this project are available on my GitHub, which is linked in 1.4.
This book serves as a collection of R Markdown files helps users in learning the practical syntax and usage of R for data science. Mainly, code snippets and workflow aimed at tackling everyday tasks in data science will be covered, which includes data cleaning, data wrangling, iterations, machine learning with caret
, data visualization, and web app design using Shiny
. Each broad topic will be split into chapters, though there will be some overlap.
1.1 Preface
This book assumes readers have a basic grasp of R programming language. There are tons of useful resources that provides the first few steps into programming with R and I hope this book can serve as a useful complement to using R - a pocket reference of sorts!
It depends on who you ask, but many users emphasize the importance of learning the syntax of base R before diving into commonly used packages like dplyr
and data.table
. It is definitely a good idea to get the hang of base R if you’re developing an app or a package for example - this would lessen the number of dependencies your program has!
However, similarly to pandas
in Python, the popularity of dplyr
and associated packages within the tidyverse
suite has soared and you wouldn’t be surprised to see tidyverse
solutions as the top answers in forums like Stack Overflow (this can be frustrating if you’re a base R purist). Using tidyverse
for data science can definitely make your life easy - I find their syntax more pretty intuitive - but I’d like to sit on the fence on the base R vs. tidyverse
debate; you should know both! For that reason, in this book I will try to use both interchangeably.
1.2 R syntax in this book
Code chunks will be presented in a typical Markdown format as such, with the code output below:
## [1] 15.9673980 14.4515851 14.9180392 51.4434260 49.2827306
## [6] 61.6342769 44.7422891 5.5676715 0.5396310 22.1834201
## [11] 85.0963224 26.7346206 59.8600273 60.8599697 99.2158440
## [16] 19.1189961 75.3390585 24.2387337 32.7452220 0.3535495
When using commands outside of base R, the loading of the parent package will be explicitly shown to avoid confusion:
## Unit: nanoseconds
## expr min lq mean median uq max
## runif(n = 20, min = 0, max = 100) 902 1066 1215.24 1107 1230 5412
## neval
## 100
Typically in longer chains of code, I will use %>%
from magrittr
as a pipe. This is usually standard practice in code using packages from the tidyverse
so it’s a good habit to start using it. However, keep in mind - as of the recent R version (shown below), there is a native R pipe |>
which behave almost - but not always - in a similar fashion.
Finally, here is the R version I am currently using:
## _
## platform aarch64-apple-darwin20
## arch aarch64
## os darwin20
## system aarch64, darwin20
## status
## major 4
## minor 3.1
## year 2023
## month 06
## day 16
## svn rev 84548
## language R
## version.string R version 4.3.1 (2023-06-16)
## nickname Beagle Scouts
1.3 R packages commonly used in this book
tidyverse
: a collection of packages for data science, includingdplyr
,purrr
,stringr
,forcats
,readr
, andggplot
.caret
: package for implementation of machine learning models, with support for algorithms such asranger
,rpart
,xgbTree
, andsvmLinear
.mlbench
: package for benchmarks and datasets in machine learning.broom
: package for summarizing of model estimates.ggpubr
: package for publication-ready data visualizations.Shiny
: package for implementation and designing of interactive web apps.
1.4 Installing R packages
R packages found in this book are available on CRAN and thus can be installed simply by running install.packages()
. For packages not on CRAN (or if you want to download developmental versions of a package), you can install packages straight from a GitHub repository by running devtools::install_github()
.
1.5 Code availability
All code used to compile this book as well as the individual markdown files are available on my repository here