R · programming language

Also known as: R language

On this page 7

R is an open-source programming language and interactive environment designed for statistical computing, data analysis and graphics. Among programming languages it occupies a specialized niche: it treats vectors, data frames and statistical models as first-class objects, so an analyst can load data, fit a model and plot the result in a few lines. The R language descends from S, a language created at Bell Laboratories, and is maintained by the R Core Team under the GNU General Public License.

Within the wider landscape of programming, R is best understood as a domain-specific general language: it is Turing-complete and can build web applications or command-line tools, yet its design, libraries and community are organized around statistics and research workflows. Not to be confused with the single letter used for other technical meanings, this page covers only the programming language.

How the R language works

R is an interpreted, dynamically typed language in which every value is an object and almost every operation is a function call. Code typed at the console is parsed into an expression tree and evaluated immediately, which makes R well suited to exploratory, interactive analysis rather than compiled, long-running services.

Three design choices define how R code behaves. First, vectorization: arithmetic and comparison operators apply element by element to whole vectors, so loops are often unnecessary. Second, copy-on-modify semantics: functions receive values that behave as copies, which protects the caller but can multiply memory use. Third, lazy evaluation of arguments: a function argument is computed only when the function body uses it, which enables formula notation and non-standard evaluation in packages such as dplyr.

The following table summarizes the core data structures that an R programmer manipulates daily.

StructureDimensionsElement typesTypical use
Atomic vectorOneSingle typeMeasurements, labels, logical masks
ListOneMixed, nestedModel output, JSON-like records
MatrixTwoSingle typeLinear algebra, distance tables
Data frameTwoOne type per columnTabular datasets, survey results
FactorOneInteger codes with levelsCategorical variables in models
Core data structures in R

Behind the console sits a C and Fortran core. Numerically intensive routines, such as matrix decomposition through BLAS and LAPACK, run as compiled code, so well-vectorized R can be fast even though the interpreter itself is relatively slow.

The package ecosystem and its object systems

Much of the practical power of R comes from CRAN, the Comprehensive R Archive Network, which hosts more than 20,000 contributed packages that pass automated checks on several operating systems. Bioconductor adds a curated collection for genomics and bioinformatics, and many packages are also distributed as source repositories.

R offers several object-oriented systems that coexist, which often surprises developers who arrive from Java or Python. The table compares them.

SystemDispatch styleStrictnessCommon context
S3Generic function on class attributeInformalBase R, print and summary methods
S4Formal generics, multiple dispatchStrict class definitionsBioconductor, Matrix package
R5 reference classesMethods belong to mutable objectsModerateStateful simulations
R6 (package)Encapsulated mutable objectsModerateShiny internals, API clients
S7 (newer package)Formal yet lightweight genericsStrictSuccessor intended to unify S3 and S4
Object systems available to R programmers

A large share of modern code follows the tidyverse conventions, a family of packages including ggplot2, dplyr and tidyr that share a grammar of tidy data, where each variable is a column and each observation is a row. Other analysts prefer base R or the high-performance data.table package, and teams should agree on one dialect to keep code readable.

How R compares with other data languages

R competes most directly with Python and MATLAB, and choosing among them depends on the task rather than on general superiority. The comparison below reflects common practice rather than absolute limits.

CriterionRPythonMATLAB
Primary strengthStatistics and visualizationGeneral-purpose and machine learningNumerical engineering and signal processing
LicenseFree, GPLFree, PSF licenseCommercial
Statistical model coverageVery broad, often first to publishBroad, growingToolbox dependent
Production deploymentPossible, less commonVery commonMostly embedded or compiled
IndexingStarts at 1Starts at 0Starts at 1
R compared with neighboring languages for data work

A practical point is that new statistical methods frequently appear first as R packages, because academic statisticians publish reference implementations alongside papers. Conversely, deep learning frameworks and general software engineering tooling are more mature in Python, so many teams run both languages side by side, exchanging data through formats such as Parquet or through bridges such as reticulate.

Typical uses and tooling

R is used wherever reproducible quantitative analysis matters: clinical trials, epidemiology, econometrics, ecology, finance, survey research and marketing analytics. Pharmaceutical teams also use it for regulatory submissions, where validated packages and documented environments are required.

The surrounding tools turn the language into a complete workflow, as the decision table shows.

NeedRecommended toolReason
Interactive codingRStudio or Positron IDEConsole, plots and data viewer in one window
Reproducible reportsR Markdown or QuartoCode, output and prose rendered together
Interactive dashboardsShinyWeb apps written entirely in R
Locked dependency versionsrenvProject-level package snapshots
Speeding up hot loopsRcppInline C++ compiled into R functions
Package developmentdevtools and testthatStandard build, document and test cycle
Choosing R tooling for common needs

Limitations and common mistakes

The main weakness of R is memory and speed at scale. Standard data frames live entirely in RAM, and copy-on-modify can duplicate large objects, so datasets of tens of gigabytes usually require data.table, Arrow, database back ends or Spark connectors.

  • Growing vectors inside loops forces repeated reallocation; preallocate or use vectorized functions instead.
  • Implicit type coercion silently turns a numeric column into character when one value is malformed.
  • 1-based indexing and negative indices that drop elements confuse programmers coming from C-family languages.
  • Unpinned package versions break analyses months later; snapshot environments with renv.
  • Mixing dialects of base R, tidyverse and data.table in one script makes code hard to review.

R also has a reputation for inconsistent naming in its base library, a legacy of decades of incremental growth. Modern style guides and linters reduce the problem, but newcomers should expect a learning curve that is steeper for software engineering than for statistics.

In everyday language

R is a free tool for asking questions of data. You give it a table of numbers, such as sales per month or patient test results, and with short commands it can calculate averages, test whether a difference is real or just chance, and draw clear charts.

Think of it as a very powerful scientific calculator with a huge library of add-ons. Researchers around the world share new methods as add-on packages, so R often gets the newest statistical techniques before any other tool.

Seen in the real world

A public health analyst at a regional health agency receives weekly case counts for a respiratory infection from 40 clinics. Using R, the analyst imports the files with readr, cleans inconsistent clinic codes with dplyr, and fits a negative binomial regression that adjusts for population size and seasonality.

The results feed a Quarto report that regenerates automatically every Monday, with ggplot2 maps showing which districts exceed the expected range. Because package versions are pinned with renv, an auditor can rerun the analysis a year later and obtain identical numbers, and the agency uses the report to send extra vaccination staff to the two districts flagged above threshold.