---
title: R (programming language)
slug: r-programming-language
qid: 
snapshot: enwiki-001378604952
content_hash: 
license: CC BY-SA 4.0
source: https://en.wikipedia.org/w/index.php?oldid=1378604952
tags: [109577999, 20161863, 20349709, 22931402, 22985424, 23118434, 23515626, 23528933, 23528938, 23603337, 24153109, 24153113, 24745122, 24769763, 26898194, 27320558, 29176646, 3469195, 37390180, 41941517, 44446176, 58680760, 69979135, 80255389, 80930309, 82546460, 90989468, 91023494]
---

# R (programming language)

> [!note] Provenance
> Snapshot `enwiki-001378604952` · content root `…`
> Derived from <https://en.wikipedia.org/w/index.php?oldid=1378604952> · Licensed CC BY-SA 4.0

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 (according to the authors and users). 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 (including Linux, MacOS, and Microsoft Windows). 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 (an integrated development environment), Jupyter (a notebook interface), as well as Termux and Google Colab for mobile devices.

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 (according to the authors and users). 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 (including Linux, MacOS, and Microsoft Windows). 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 (an integrated development environment), Jupyter (a notebook interface), as well as Termux and Google Colab for mobile devices. == Packages == created with the R package ggplot2 for data visualization R packages are collections of functions, documentation, and data that expand R. For example, packages can add reporting features (using packages such as R Markdown, Quarto, knitr, and Sweave) and support for various statistical techniques (such as linear, generalized linear and nonlinear modeling, classical statistical tests, spatial analysis, time-series analysis, and clustering). 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 MASS package, which bundles functions and datasets to support the textbook Modern Applied Statistics with S by Venables and Ripley. Installing a package occurs only once. For example, to install the MASS package: > install.packages("MASS") To load the functions, data, and documentation of a package, one calls the library() function. To load the MASS collection, one can execute the following code: > # The package name can be enclosed in quotes > library("MASS") > # But the package name can also be used without quotes > library(MASS) The Comprehensive R Archive Network (CRAN) 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 (CTAN) and the Comprehensive Perl Archive Network (CPAN). 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 Task Views 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. > print("Hello, World!") [1] "Hello, World!" Here is an alternative version, which uses the cat() function: > cat("Hello, World!") Hello, World! In R, the generally preferred assignment operator is an arrow made from two characters , although = can be used in some cases. > x y y # Print the vector’s contents. [1] 1 4 9 16 25 36 > z z # Return the contents of z to the current environment. [1] 2 6 12 20 30 42 > z_matrix z_matrix [,1] [,2] [1,] 2 20 [2,] 6 30 [3,] 12 42 > 2 * t(z_matrix) - 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. [,1] [,2] [,3] [1,] 2 10 22 [2,] 38 58 82 Create a new dataframe object that contains the data from a transposed z_matrix, with row names 'A' and 'B' > new_df names(new_df) new_df # Print the current results. X Y Z A 2 6 12 B 20 30 42 > new_df$Z # Output the Z column [1] 12 42 > new_df$Z == new_df['Z'] && new_df[3] == new_df$Z # The dataframe column Z can be accessed using the syntax $Z, ['Z'], or [3], and the values are the same. [1] TRUE > attributes(new_df) # Print information about attributes of the new_df dataframe $names [1] "X" "Y" "Z" $row.names [1] "A" "B" $class [1] "data.frame" > attributes(new_df)$row.names new_df X Y Z one 2 6 12 two 20 30 42 # The function's input parameters are x and y. The function, named f, returns a linear combination of x and y. f The following is some output from using the function defined above: > f(1, 2) # 3 * 1 + 4 * 2 = 3 + 8 [1] 11 > f(c(1, 2, 3), c(5, 3, 4)) # Element-wise calculation [1] 23 18 25 > f(1:3, 4) # Equivalent to f(c(1, 2, 3), c(4, 4, 4)) [1] 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%` 1:3 %sumx2y2% -(1:3) [1] 2 8 18 Since R version 4.1.0, functions can be written in a short notation (inspired by the lambda calculus), which is useful for passing anonymous functions to higher-order functions: > sapply(1:5, \(i) i^2) # here \(i) is the same as function(i) [1] 1 4 9 16 25 > nrow(subset(mtcars, cyl == 4)) # Nested without the pipe character [1] 11 > mtcars |> subset(cyl == 4) |> nrow() # Using the pipe character [1] 11 An alternative to nested functions is the use of intermediate objects, rather than the pipe operator: > mtcars_subset_rows num_mtcars_subset print(num_mtcars_subset) [1] 11 While the pipe operator can produce code that is easier to read, influential R programmers like Hadley Wickham suggest to chain together at most 10-15 lines of code using this operator and saving them into objects having meaningful names to avoid code obfuscation. > data summary(data) Length Class Mode 5 character character > summary(as.factor(data)) a b c NA's 2 1 1 1 Create x and y values x 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(>|t|) (Intercept) -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 This example of a Mandelbrot set highlights the use of complex numbers. It models the first 20 iterations of the equation z = z 2 + 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("caTools") The sample code is as follows: library(caTools) jet.colors Vim via the Nvim-R plugin Kate LyX via Sweave WinEdt (website) Jupyter (website) Other scripting languages: Python (website) Perl (website) Ruby (source code) F# (website) Julia (source code). General-purpose programming languages: Java via the Rserve socket server .NET C# (website) Statistical frameworks that use R in the background include Jamovi and JASP. ==Notes==
