R Basics
In this part, we provide a brief introduction to R for econometric analysis. This part consists of nine chapters:
- Chapter 1 Introduction to R
- Chapter 2 Data Structures in R
- Chapter 3 Control Structures
- Chapter 4 Custom Functions
- Chapter 5 Statistical Functions
- Chapter 6 Graphics
- Chapter 7 Dataframes
- Chapter 8 Applications in Numerical Analysis
- Chapter 9 Object-Oriented Programming
In Chapter 1 Introduction to R, we begin by showing how to install R and provide a brief overview of its history. We then explain how to install and use user-contributed packages. Next, we introduce two IDEs: RStudio and Positron. We also briefly demonstrate how to use R as a calculator and how to create objects and functions. The chapter concludes with a discussion of how to use documentation and how to set the working directory.
In Chapter 2 Data Structures in R, we introduce basic data containers and data types. We cover vectors, matrices, arrays, and lists, along with useful functions that operate on these objects. We then discuss data types, including numerical data, booleans, strings, and dates and times. Finally, we show how to handle missing values.
In Chapter 3 Control Structures, we cover control structures in R. We discuss conditional statements (if, ifelse, if...else) , loops (for, while, repeat), and the switch statement (switch) for efficient iteration tasks.
In Chapter 4 Custom Functions, we introduce how to write efficient custom functions using the function keyword. We discuss the syntax, including how to pass arguments, return values, and use default argument values.
In Chapter 5 Statistical Functions, we introduce statistical functions from R for generating random draws from well-known distributions. We show how to evaluate density and cumulative distribution functions, and compute quantiles and other key statistics.
In Chapter 6 Graphics, we introduce the base R and tidyverse approaches for graphics. We cover widely used plot types, including scatter plots, line plots, bar plots, and histograms. We also discuss how to customize plots with titles, labels, and themes.
In Chapter 7 Dataframes, we introduce data frames for data management using both base R and tidyverse principles. We show how to import data from CSV files, Stata .dta files, Excel spreadsheets, and other formats. We cover methods for filtering, sorting, grouping, merging data, and handling missing values.
In Chapter 8 Applications in Numerical Analysis, we cover some useful numerical methods for statistics and econometrics. We discuss optimization methods, numerical integration methods, and methods for solving ordinary differential equations.
Finally, in Chapter 9 Object-Oriented Programming, we introduce the basics of object-oriented programming (OOP) in R based on the R6 system. We explain how to define classes with fields and methods, create objects, and use inheritance to build new classes from existing ones.
The material in this part mainly builds on Jones, Harden, and Crawley (2022) for the base R approach and on H. Wickham (2019) and Hadley Wickham, Grolemund, and Çetinkaya-Rundel (2023) for the tidyverse approach. For further details, readers are referred to the corresponding chapters in these references.