An Introduction to R for Research
Welcome
Software information and conventions
Acknowledgments
About the Author
1
Introduction
1.1
Installing R
1.2
Installing RStudio
1.3
R and RStudio Basics
1.3.1
Running R code
1.3.2
Some definitions
1.3.3
Assigning a value to an object
1.3.4
Comment code
1.3.5
Navigating a script file with headings in R Studio
1.3.6
Installing and loading packages
1.3.7
The R environment
1.3.8
Working directory
1.3.9
Saving and loading objects
1.3.10
R Projects
1.4
Keyboard shortcuts
1.5
Value types
1.6
R as a calculator
1.7
Object types
1.7.1
Numeric single value
1.7.2
Character single value
1.7.3
Vector
1.7.4
Factor
1.7.5
Matrix
1.7.6
Array
1.7.7
List
1.7.8
Data frame
1.7.9
Converting between types
1.8
Extracting / subsetting
1.8.1
Extracting elements from a vector
1.8.2
Extracting elements from a matrix or array
1.8.3
Extracting elements from a list
1.8.4
Extracting elements from a data frame
1.9
Missing and infinite values
1.10
Naming objects
1.11
Basic operations
1.12
Matrix operators
1.13
Logical operators
1.14
Iteration using a for loop
1.15
Functions
1.16
Creating a function
1.17
Neat coding
1.18
R tidyverse
1.19
Be a lifelong R learner
1.20
Datasets used in this book
1.20.1
Arthritis Treatment
1.20.2
2013-2014 National Health and Nutrition Examination Survey (NHANES)
1.20.3
2017 Youth Risk Behavior Surveillance System (YRBSS)
1.20.4
Caveat regarding survey weights
2
Import data and check for problems
2.1
Definitions
2.2
Loading an R dataset
2.3
Importing a file from another format
2.3.1
Delimited .txt file
2.3.1.1
From Text (base) (base R)
2.3.1.2
From Text (redr) (tidyverse)
2.3.2
Comma delimited .csv file
2.3.2.1
Base R
2.3.2.2
Tidyverse
2.3.3
Excel (.xls, .xlsx)
2.3.4
SPSS file (.sav)
2.3.5
SAS file (.sas7bdat)
2.3.6
Stata file (.dta)
2.4
Converting between base R
data.frame
and tidyverse
tibble
2.5
When to use
file.choose()
2.6
Inspecting the data
2.6.1
Base R
2.6.1.1
Tabular inspection
2.6.1.2
Graphical inspection
2.6.2
Tidyverse
2.6.2.1
Tabular inspection
2.6.2.2
Graphical inspection
2.7
Saving a dataset in R format
2.8
Exercise
2.9
For further study
3
Manage variables within a dataset
3.1
Fix mis-spellings or other invalid values
3.2
Use factors for categorical variables
3.3
Collapse a categorical variable into fewer levels
3.4
Transform a numeric variable
3.5
Convert numeric to binary
3.6
Convert numeric to categorical by binning
3.6.1
Specific ranges
3.6.2
Equal length bins
3.6.3
Equal size bins
3.7
Set values to missing
3.8
Rename variables
3.9
Exercise
4
Manage datasets
4.1
Select a subset of variables
4.2
Select a subset of observations
4.3
Exclude observations with missing data
4.4
Sort
4.5
Append (stack)
4.6
Merge (join)
4.6.1
Example of a one-to-many merge
4.7
For further study:
4.8
Exercise
5
Descriptive statistics
5.1
Summarizing categorical data
5.2
Interlude – Examining and debugging a function
5.3
Summarizing continuous data
5.3.1
Mean or Median: Examining symmetry
5.4
By another variable
5.4.1
tapply
5.4.2
group_by() + summarize()
5.5
Table 1
6
Basic data visualization
6.1
Categorical: Bar chart
6.1.1
Base R
6.1.2
ggplot
6.2
Continuous: Histogram
6.2.1
Base R
6.2.2
ggplot
6.3
Continuous: Boxplot
6.3.1
Base R
6.3.2
ggplot
6.4
Categorical vs Categorical: Clustered bar charts
6.4.1
Base R
6.4.2
ggplot
6.5
Continuous vs. Categorical
6.5.1
Bar chart with mean and SD by group
6.5.2
Side-by-side boxplots
6.5.2.1
Base R
6.5.2.2
ggplot
6.5.3
Rows of histograms
6.5.3.1
Base R
6.5.3.2
ggplot
6.6
Continuous vs. Continuous: Scatterplot with optional regression line
6.6.1
Base R
6.6.2
ggplot
6.7
Three or more continuous variables: Scatterplot matrix
6.7.1
Base R
6.7.2
ggplot
6.8
Customizing
6.8.1
Base R
6.8.2
ggplot
6.9
Exporting figures
6.10
For further study
References
Published with bookdown
An Introduction to R for Research
About the Author
Ramzi W. Nahhas