🔒

R Scripts — R Basics

Enter the password to access this page.

📘

R Basics

Core R syntax — objects, vectors, data frames, control flow and functions

Assignment and basic types
Assign values with <- or =. R has four atomic types: numeric, character, logical and integer. Use
c(), seq(), rep()
Vectors are the fundamental data structure in R. All elements must be the same type. Key creation fu
data.frame(), str(), head()
Data frames are the standard tabular structure — a list of equal-length vectors. Each column can hav
[ ], [[ ]], $, subset()
R uses [ for subsetting (returns same type), [[ for extracting single elements, and $ for named colu
if / else, for, while, break, next
Standard control structures. In R, for loops iterate over any vector. Prefer vectorised operations o
function(args) { body }
Functions are first-class objects in R. Define with function(). The last evaluated expression is ret
apply(), lapply(), sapply(), tapply()
The apply family applies a function over margins of arrays, lists or grouped data — cleaner and ofte
paste(), gsub(), grep(), substr()
Base R string functions cover most needs. paste() and paste0() concatenate; gsub()/sub() replace pat
read.csv(), write.csv(), readRDS(), saveRDS()
Base R handles CSV, TSV and R binary objects (RDS). For Excel files use readxl::read_excel(); for la
plot(), hist(), boxplot(), barplot()
Base R graphics are quick for exploration. plot(x, y) for scatter, hist() for distribution, boxplot(