13  Data Structures

13.1 Data Types in R

Type Description Example
Numeric Decimal or integer numbers 12.5 (hectares), 340 (kg)
Character Text strings "wheat", "Field-A"
Logical TRUE / FALSE values soil_tested <- TRUE
Integer Whole numbers, marked with L plot_count <- 6L
Factor Categorical values with fixed levels soil_type <- factor("loam")

13.2 Assignment and Basic Operations

<- (preferred) or = assigns a value to a name. A comment (#) documents what a line of code does — worth using generously, since a script you wrote three seasons ago is only useful if you can still tell what it does.

13.3 Arithmetic Operators

Operator Meaning Example
+ Addition yield_a + yield_b
- Subtraction total_cost - subsidy
* Multiplication price_per_kg * quantity_kg
/ Division total_yield / hectares
^ Exponentiation — raises the first number to the power of the second 2^3 returns 8, not 4
%% Modulus — the remainder after division 17 %% 5 returns 2
%/% Integer division 17 %/% 5 returns 3

13.4 Vectors

A vector is a one-dimensional sequence of values of the same type, created with c() (combine).

13.5 Matrix

A matrix is a two-dimensional structure of values of the same type, arranged in rows and columns.

13.6 Array

An array extends a matrix to more than two dimensions — useful, for example, when tracking a measurement across fields, months, and years at once.

Summary

Concept Description
R Data Structures
Data Types in R Numeric, character, logical, integer, and factor — the core building blocks of every R object.
Assignment and Basic Operations `<-` or `=` binds a value to a name; `#` starts a comment.
Arithmetic Operators Standard arithmetic, including `^` for exponentiation (not squaring) and `%%`/`%/%` for remainder and integer division.
Vectors One-dimensional sequences of same-type values, created with `c()`.
Matrix Two-dimensional grids of same-type values, arranged in rows and columns.
Array Extends a matrix beyond two dimensions, e.g. field × time × year.