# The code below calculates 1 + 2
1 + 2[1] 3
Traditionally, data analyses were conducted using software such as SPSS, Stata, and XLSTAT. However, the rise of R introduced a new paradigm for analyzing and visualizing data. Unlike those point-and-click tools, R is a programming language—and programming is a subject that is rarely included in traditional science curricula.
Learning a programming language like R often requires unlearning how computers are typically used. With touchscreens, mice, voice commands, and artificial intelligence (where human-computer interaction feels more like a standard conversation), typing commands directly into a keyboard can feel obsolete.
The Online Historical Encyclopaedia of Programming Languages (HOPL) records over 8,900 languages. Because most programming languages share the same core principles, understanding the basics makes transitioning between languages much easier. Although R is the only language used in this handbook, the general principles of programming are explained using R as a concrete example.
This section introduces the basic framework of computer programming, describing the console (26 Console), the script editor (27 Script editor), the Integrated Development Environment (28 IDE: Integrated Development Environment), and the concept of variable assignment (29 Variable assigment), and a function (40 Function).
It describes the elementary data types in programming: numbers (30 Type: Integer and numeric), strings or characters (32 Type: String or Character), and booleans (33 Type: Boolean), and the data structures built from these basic types to allow more flexible storage: vectors (35 Vector), matrices (36 Matrix), data frames (38 Data frame), and lists (39 List).
A series of chapters introduces the syntax to perform arithmetic (31 Arithmetic operation), matrix operations (37 Vector and matrix operation), and boolean operations (34 Boolean operation) in any programming languages.
Finally, two chapters cover two daily activities of a programmer: writing pseudocode (41 Pseudocode) and identifying and fixing bugs (42 Debugging).
Unlike previous sections, this section includes code snippets and their outputs. Code appears in light gray blocks, with the output displayed underneath in a distinct black font. Comments within the code start with a # symbol to explain what the lines do. Here is an example:
# The code below calculates 1 + 2
1 + 2[1] 3
Practicing by copying, typing, and modifying these code examples is strongly recommended.