R (programming language)
R is a programming language for statistical computing and data visualization. It has been widely adopted in the fields of data mining, bioinformatics, data analysis, and data science.
The core R language is extended by a large number of software packages, which contain reusable code, documentation, and sample data. Some of the most popular R packages are in the tidyverse collection, which enhances functionality for visualizing, transforming, and modelling data, as well as improves the ease of programming.
R is free and open-source software distributed under the GNU General Public License. The language is implemented primarily in C, Fortran, and R itself. Precompiled executables are available for the major operating systems.
Its core is an interpreted language with a native command line interface. In addition, multiple third-party applications are available as graphical user interfaces; such applications include RStudio, Jupyter, as well as Termux and Google Colab for mobile devices.
History
R was started by professors Ross Ihaka and Robert Gentleman as a programming language to teach introductory statistics at the University of Auckland. The language was inspired by the S programming language, with most S programs able to run unaltered in R. The language was also inspired by Scheme's lexical scoping, allowing for local variables.The name of the language, R, comes from being both an S language successor and the shared first letter of the authors, Ross and Robert. In August 1993, Ihaka and Gentleman posted a binary file of R on StatLib — a data archive website. At the same time, they announced the posting on the s-news mailing list. On 5 December 1997, R became a GNU project when version 0.60 was released. On 29 February 2000, the 1.0 version was released.
Packages
R packages are collections of functions, documentation, and data that expand R. For example, packages can add reporting features and support for various statistical techniques. Ease of package installation and use have contributed to the language's adoption in data science.Immediately available when starting R after installation, base packages provide the fundamental and necessary syntax and commands for programming, computing, graphics production, basic arithmetic, and statistical functionality.
An example is the tidyverse collection of R packages, which bundles several subsidiary packages to provide a common API. The collection specializes in tasks related to accessing and processing "tidy data", which are data contained in a two-dimensional table with a single row for each observation and a single column for each variable.
Installing a package occurs only once. For example, to install the tidyverse collection:
> install.packages
To load the functions, data, and documentation of a package, one calls the
library function. To load the tidyverse collection, one can execute the following code:> # The package name can be enclosed in quotes
> library
> # But the package name can also be used without quotes
> library
The Comprehensive R Archive Network was founded in 1997 by Kurt Hornik and Friedrich Leisch to host R's source code, executable files, documentation, and user-created packages. CRAN's name and scope mimic the Comprehensive TeX Archive Network and the Comprehensive Perl Archive Network. CRAN originally had only three mirror sites and twelve contributed packages., it has 90 mirrors and 22,390 contributed packages. Packages are also available in repositories such as R-Forge, Omegahat, and GitHub.
To provide guidance on the CRAN web site, its area lists packages that are relevant for specific topics; sample topics include causal inference, finance, genetics, high-performance computing, machine learning, medical imaging, meta-analysis, social sciences, and spatial statistics.
The Bioconductor project provides packages for genomic data analysis, complementary DNA, microarray, and high-throughput sequencing methods.
Community
There are three main groups that help support R software development:- The R Core Team was founded in 1997 to maintain the R source code.
- The R Foundation for Statistical Computing was founded in April 2003 to provide financial support.
- The R Consortium is a Linux Foundation project to develop R infrastructure.
The R community hosts many conferences and in-person meetups. These groups include:
- UseR!: an annual international R user conference
- Directions in Statistical Computing
- R-Ladies: an organization to promote gender diversity in the R community
- SatRdays: R-focused conferences held on Saturdays
- Data Science & AI Conferences
- posit::conf
#rstats can be used to follow new developments in the R community.Examples
Hello, World!
The following is a "Hello, World!" program:"Hello, World!"
cat function:> cat
Hello, World!
Basic syntax
The following examples illustrate the basic syntax of the language and use of the command-line interface.In R, the generally preferred assignment operator is an arrow made from two characters
<-, although = can be used in some cases.> x <- 1:6 # Create a numeric vector in the current environment
> y <- x^2 # Similarly, create a vector based on the values in x.
> y # Print the vector’s contents.
1 4 9 16 25 36
> z <- x + y # Create a new vector that is the sum of x and y
> z # Return the contents of z to the current environment.
2 6 12 20 30 42
> z_matrix <- matrix # Create a new matrix that transforms the vector z into a 3x2 matrix object
> z_matrix
2 20
6 30
12 42
> 2 * t - 2 # Transpose the matrix; multiply every element by 2; subtract 2 from each element in the matrix; and then return the results to the terminal.
2 10 22
38 58 82
> new_df <- data.frame, row.names = c) # Create a new dataframe object that contains the data from a transposed z_matrix, with row names 'A' and 'B'
> names <- c # Set the column names of the new_df dataframe as X, Y, and Z.
> new_df # Print the current results.
X Y Z
A 2 6 12
B 20 30 42
> new_df$Z # Output the Z column
12 42
> new_df$Z new_df && new_df new_df$Z # The dataframe column Z can be accessed using the syntax $Z,, or, and the values are the same.
TRUE
> attributes # Print information about attributes of the new_df dataframe
$names
"X" "Y" "Z"
$row.names
"A" "B"
$class
"data.frame"
> attributes$row.names <- c # Access and then change the row.names attribute; this can also be done using the rownames function
> new_df
X Y Z
one 2 6 12
two 20 30 42
Structure of a function
R is able to create functions that add new functionality for code reuse. Objects created within the body of the function remain accessible only from within the function, and any data type may be returned. In R, almost all functions and all user-defined functions are closures.The following is an example of creating a function to perform an arithmetic calculation:
- The function, named f, returns a linear combination of x and y.
- As an alternative, the last statement executed in a function is returned implicitly.
The following is some output from using the function defined above:
> f # 3 * 1 + 4 * 2 = 3 + 8
11
> f, c) # Element-wise calculation
23 18 25
> f # Equivalent to f, c)
19 22 25
It is possible to define functions to be used as infix operators by using the special syntax
`%name%`, where "name" is the function variable name:> `%sumx2y2%` <- function
> 1:3 %sumx2y2% -
2 8 18
Since R version 4.1.0, functions can be written in a short notation, which is useful for passing anonymous functions to higher-order functions:
> sapply # here \ is the same as function
1 4 9 16 25
Native pipe operator
In R version 4.1.0, a native pipe operator,|>, was introduced. This operator allows users to chain functions together, rather than using nested function calls.> nrow # Nested without the pipe character
11
> mtcars |> subset |> nrow # Using the pipe character
11
An alternative to nested functions is the use of intermediate objects, rather than the pipe operator:
> mtcars_subset_rows <- subset
> num_mtcars_subset <- nrow
11
Object-oriented programming
The R language has native support for object-oriented programming. There are two native frameworks, the so-called S3 and S4 systems. The former, being more informal, supports single dispatch on the first argument, and objects are assigned to a class simply by setting a "class" attribute in each object. The latter is a system like the Common Lisp Object System (CLOS), with formal classes and generic methods, which supports multiple dispatch and multiple inheritanceIn the example below,
summary is a generic function that dispatches to different methods depending on whether its argument is a numeric vector or a factor:> data <- c
> summary
Length Class Mode
5 character character
> summary
a b c NA's
2 1 1 1
Modeling and plotting
The R language has built-in support for data modeling and graphics. The following example shows how R can generate and plot a linear model with residuals.- Create x and y values
y <- x^2
- Linear regression model: y = A + B * x
- Display an in-depth summary of the model
- Create a 2-by-2 layout for figures
- Output diagnostic plots of the model
The output from the
summary function in the preceding code block is as follows:Residuals:
1 2 3 4 5 6 7 8 9 10
3.3333 -0.6667 -2.6667 -2.6667 -0.6667 3.3333
Coefficients:
Estimate Std. Error t value Pr
-9.3333 2.8441 -3.282 0.030453 *
x 7.0000 0.7303 9.585 0.000662 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.055 on 4 degrees of freedom
Multiple R-squared: 0.9583, Adjusted R-squared: 0.9478
F-statistic: 91.88 on 1 and 4 DF, p-value: 0.000662
Mandelbrot set
This example of a Mandelbrot set highlights the use of complex numbers. It models the first 20 iterations of the equationz = z2 + c, where c represents different complex constants.To run this sample code, it is necessary to first install the package that provides the
write.gif function:install.packages
The sample code is as follows:
jet.colors <-
colorRampPalette
dx <- 1500 # define width
dy <- 1400 # define height
C <-
complex
- reshape as matrix of complex numbers
- initialize output 3D array
Z <- 0
- loop with 20 iterations
write.gif
Version names
All R version releases from 2.14.0 onward have codenames that make reference to Peanuts comics and films.In 2018, core R developer Peter Dalgaard presented a history of R releases since 1997. Some notable early releases before the named releases include the following:
- Version 1.0.0, released on 29 February 2000, a leap day
- Version 2.0.0, released on 4 October 2004, "which at least had a nice ring to it"
| Version | Release date | Name | Peanuts reference | Reference |
| 4.5.2 | 2025-10-31 | Part in a Rumble | ||
| 4.5.1 | 2025-06-13 | Great Square Root | ||
| 4.5.0 | 2025-04-11 | How About a Twenty-Six | ||
| 4.4.3 | 2025-02-28 | Trophy Case | ||
| 4.4.2 | 2024-10-31 | Pile of Leaves | ||
| 4.4.1 | 2024-06-14 | Race for Your Life | ||
| 4.4.0 | 2024-04-24 | Puppy Cup | ||
| 4.3.3 | 2024-02-29 | Angel Food Cake | ||
| 4.3.2 | 2023-10-31 | Eye Holes | ||
| 4.3.1 | 2023-06-16 | Beagle Scouts | ||
| 4.3.0 | 2023-04-21 | Already Tomorrow | ||
| 4.2.3 | 2023-03-15 | Shortstop Beagle | ||
| 4.2.2 | 2022-10-31 | Innocent and Trusting | ||
| 4.2.1 | 2022-06-23 | Funny-Looking Kid | ||
| 4.2.0 | 2022-04-22 | Vigorous Calisthenics | ||
| 4.1.3 | 2022-03-10 | One Push-Up | ||
| 4.1.2 | 2021-11-01 | Bird Hippie | ||
| 4.1.1 | 2021-08-10 | Kick Things | ||
| 4.1.0 | 2021-05-18 | Camp Pontanezen | ||
| 4.0.5 | 2021-03-31 | Shake and Throw | ||
| 4.0.4 | 2021-02-15 | Lost Library Book | ||
| 4.0.3 | 2020-10-10 | Bunny-Wunnies Freak Out | ||
| 4.0.2 | 2020-06-22 | Taking Off Again | ||
| 4.0.1 | 2020-06-06 | See Things Now | ||
| 4.0.0 | 2020-04-24 | Arbor Day | ||
| 3.6.3 | 2020-02-29 | Holding the Windsock | ||
| 3.6.2 | 2019-12-12 | Dark and Stormy Night | See It was a dark and stormy night#Literature | |
| 3.6.1 | 2019-07-05 | Action of the Toes | ||
| 3.6.0 | 2019-04-26 | Planting of a Tree | ||
| 3.5.3 | 2019-03-11 | Great Truth | ||
| 3.5.2 | 2018-12-20 | Eggshell Igloos | ||
| 3.5.1 | 2018-07-02 | Feather Spray | ||
| 3.5.0 | 2018-04-23 | Joy in Playing | ||
| 3.4.4 | 2018-03-15 | Someone to Lean On | ||
| 3.4.3 | 2017-11-30 | Kite-Eating Tree | See Kite-Eating Tree | |
| 3.4.2 | 2017-09-28 | Short Summer | See It Was a Short Summer, Charlie Brown | |
| 3.4.1 | 2017-06-30 | Single Candle | ||
| 3.4.0 | 2017-04-21 | You Stupid Darkness | ||
| 3.3.3 | 2017-03-06 | Another Canoe | ||
| 3.3.2 | 2016-10-31 | Sincere Pumpkin Patch | ||
| 3.3.1 | 2016-06-21 | Bug in Your Hair | ||
| 3.3.0 | 2016-05-03 | Supposedly Educational | ||
| 3.2.5 | 2016-04-11 | Very, Very Secure Dishes | ||
| 3.2.4 | 2016-03-11 | Very Secure Dishes | ||
| 3.2.3 | 2015-12-10 | Wooden Christmas-Tree | See A Charlie Brown Christmas | |
| 3.2.2 | 2015-08-14 | Fire Safety | ||
| 3.2.1 | 2015-06-18 | World-Famous Astronaut | ||
| 3.2.0 | 2015-04-16 | Full of Ingredients | ||
| 3.1.3 | 2015-03-09 | Smooth Sidewalk | ||
| 3.1.2 | 2014-10-31 | Pumpkin Helmet | See You're a Good Sport, Charlie Brown | |
| 3.1.1 | 2014-07-10 | Sock it to Me | ||
| 3.1.0 | 2014-04-10 | Spring Dance | ||
| 3.0.3 | 2014-03-06 | Warm Puppy | ||
| 3.0.2 | 2013-09-25 | Frisbee Sailing | ||
| 3.0.1 | 2013-05-16 | Good Sport | ||
| 3.0.0 | 2013-04-03 | Masked Marvel | ||
| 2.15.3 | 2013-03-01 | Security Blanket | ||
| 2.15.2 | 2012-10-26 | Trick or Treat | ||
| 2.15.1 | 2012-06-22 | Roasted Marshmallows | ||
| 2.15.0 | 2012-03-30 | Easter Beagle | ||
| 2.14.2 | 2012-02-29 | Gift-Getting Season | See It's the Easter Beagle, Charlie Brown | |
| 2.14.1 | 2011-12-22 | December Snowflakes | ||
| 2.14.0 | 2011-10-31 | Great Pumpkin | See It's the Great Pumpkin, Charlie Brown | |
| r-devel | N/A | Unsuffered Consequences |
Interfaces
R is installed with a command line console by default, but there are multiple ways to interface with the language:- Integrated development environment (IDE):
- * R.app
- * Rattle GUI
- * R Commander
- * RKWard
- * RStudio
- * Positron
- * Tinn-R
- General-purpose IDEs:
- * Eclipse via the
- * Visual Studio via R Tools for Visual Studio.
- Source-code editors:
- * Emacs
- * Vim via the
- * Kate
- * LyX via Sweave
- * WinEdt
- * Jupyter
- Other scripting languages:
- * Python
- * Perl
- * Ruby
- * F#
- * Julia.
- General-purpose programming languages:
- * Java via the
- * .NET C#
Implementations
The main R implementation is written primarily in C, Fortran, and R itself. Other implementations include the following:- , by Radford M. Neal, which attempts to improve memory management.
- Renjin for the Java Virtual Machine.
- and Riposte written in C++.
- Oracle's built on .
- TIBCO Enterprise Runtime for R to integrate with Spotfire.
Commercial support
Although R is an open-source project, some companies provide commercial support:- Oracle provides commercial support for its Big Data Appliance, which integrates R into its other products.
- IBM provides commercial support for execution of R within Hadoop.