Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
R (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == === Hello, World! === [["Hello, World!" program]]: <syntaxhighlight lang="rout">> print("Hello, World!") [1] "Hello, World!"</syntaxhighlight>Alternatively:<syntaxhighlight lang="rout"> > cat("Hello, World!") Hello, World! </syntaxhighlight> === Basic syntax === The following examples illustrate the basic [[programming language syntax|syntax of the language]] and use of the command-line interface.{{efn|An expanded list of standard language features can be found in the R manual, "An Introduction to R" {{URL|cran.r-project.org/doc/manuals/R-intro.pdf}}}} In R, the generally preferred [[Assignment (computer science)|assignment operator]] is an arrow made from two characters <code><-</code>, although <code>=</code> can be used in some cases.<ref>{{cite web|author=R Development Core Team|title=Assignments with the = Operator|url=https://developer.r-project.org/equalAssign.html|access-date=2018-09-11}}</ref> <syntaxhighlight lang="rout"> > x <- 1:6 # Create a numeric vector in the current environment > y <- x^2 # Create vector based on the values in x. > print(y) # Print the vectorβs contents. [1] 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. [1] 2 6 12 20 30 42 > z_matrix <- matrix(z, nrow = 3) # Create a new matrix that turns the vector z into a 3x2 matrix object > 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 return the results to the terminal. [,1] [,2] [,3] [1,] 2 10 22 [2,] 38 58 82 > new_df <- data.frame(t(z_matrix), row.names = c("A", "B")) # Create a new data.frame object that contains the data from a transposed z_matrix, with row names 'A' and 'B' > names(new_df) <- c("X", "Y", "Z") # Set the column names of new_df as X, Y, and Z. > print(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 data.frame column Z can be accessed using $Z, ['Z'], or [3] syntax and the values are the same. [1] TRUE > attributes(new_df) # Print attributes information about the new_df object $names [1] "X" "Y" "Z" $row.names [1] "A" "B" $class [1] "data.frame" > attributes(new_df)$row.names <- c("one", "two") # Access and then change the row.names attribute; can also be done using rownames() > new_df X Y Z one 2 6 12 two 20 30 42 </syntaxhighlight> === Structure of a function === R is able to create [[Function (computer programming)|functions]] to add new functionality for reuse.<ref>{{cite web|url=http://www.statmethods.net/management/userfunctions.html|title=Quick-R: User-Defined Functions|first=Robert|last=Kabacoff|year=2012|access-date=2018-09-28|website=statmethods.net}}</ref> [[Object (computer science)|Objects]] created within the body of the function (which are enclosed by curly brackets) remain [[Local variable|only accessible]] from within the function, and any [[data type]] may be returned. In R, almost all functions and all [[user-defined function]]s are [[closure (computer programming)|closures]].<ref>{{cite web|url=http://adv-r.had.co.nz/Functional-programming.html#closures|title=Advanced R - Functional programming - Closures|website=adv-r.had.co.nz|first=Hadley|last=Wickham}}</ref> Example of creating a function to perform some arithmetic calculation: <syntaxhighlight lang="r"># The input parameters are x and y. # The function, being named f, returns a linear combination of x and y. f <- function(x, y) { z <- 3 * x + 4 * y # An explicit return() statement is optional, could be replaced with simply `z`. return(z) } # Alternatively, the last statement executed is implicitly returned. f <- function(x, y) 3 * x + 4 * y</syntaxhighlight> Usage output: <syntaxhighlight lang="rout"> > 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 </syntaxhighlight> It is possible to define functions to be used as [[Infix notation|infix operators]] with the special syntax <code>`%name%`</code> where "name" is the function variable name: <syntaxhighlight lang="rout"> > `%sumx2y2%` <- function(e1, e2) {e1 ^ 2 + e2 ^ 2} > 1:3 %sumx2y2% -(1:3) [1] 2 8 18 </syntaxhighlight> Since version 4.1.0 functions can be written in a short notation, which is useful for passing anonymous functions to higher-order functions:<ref>{{cite web|url=https://cran.r-project.org/doc/manuals/r-release/NEWS.html#:~:text=R%20now%20provides%20a%20shorthand%20notation%20for%20creating%20functions|title=NEWS|website=r-project.org}}</ref> <syntaxhighlight lang="rout"> > sapply(1:5, \(i) i^2) # here \(i) is the same as function(i) [1] 1 4 9 16 25 </syntaxhighlight> === Native pipe operator === In R version 4.1.0, a native [[Pipeline (computing)|pipe operator]], <code>|></code>, was introduced.<ref>{{Cite web |title=R: R News |url=https://cran.r-project.org/doc/manuals/r-devel/NEWS.html |access-date=2024-03-14 |website=cran.r-project.org}}</ref> This operator allows users to chain functions together one after another, instead of a nested function call. <syntaxhighlight lang="rout"> > nrow(subset(mtcars, cyl == 4)) # Nested without the pipe character [1] 11 > mtcars |> subset(cyl == 4) |> nrow() # Using the pipe character [1] 11 </syntaxhighlight> Another alternative to nested functions, in contrast to using the pipe character, is using intermediate objects: <syntaxhighlight lang="rout"> > mtcars_subset_rows <- subset(mtcars, cyl == 4) > num_mtcars_subset <- nrow(mtcars_subset_rows) > print(num_mtcars_subset) [1] 11 </syntaxhighlight>While the pipe operator can produce code that is easier to read, it has been advised to pipe together at most 10 to 15 lines and chunk code into [[Task (project management)|sub-tasks]] which are saved into objects with meaningful names.<ref>{{Cite book |last=Wickham |first=Hadley |url=https://r4ds.hadley.nz/ |title=R for data science: import, tidy, transform, visualize, and model data |last2=Γetinkaya-Rundel |first2=Mine |last3=Grolemund |first3=Garrett |date=2023 |publisher=O'Reilly |isbn=978-1-4920-9740-2 |edition=2nd |location=Beijing; Sebastopol, CA |chapter=4 Workflow: code style |oclc=on1390607935 |chapter-url=https://r4ds.hadley.nz/workflow-style.html}}</ref> Here is an example with fewer than 10 lines that some readers may still struggle to grasp without intermediate named steps:<syntaxhighlight lang="r" line="1">(\(x, n = 42, key = c(letters, LETTERS, " ", ":", ")")) strsplit(x, "")[[1]] |> (Vectorize(\(chr) which(chr == key) - 1))() |> (`+`)(n) |> (`%%`)(length(key)) |> (\(i) key[i + 1])() |> paste(collapse = "") )("duvFkvFksnvEyLkHAErnqnoyr")</syntaxhighlight> === Object-oriented programming === The R language has native support for [[object-oriented programming]]. There are two native [[Application framework|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 by just setting a "class" attribute in each object. The latter is a [[CLOS|Common Lisp Object System (CLOS)-like system]] of formal classes (also derived from [[S (programming language)#S4|S]]) and generic methods that supports [[multiple dispatch]] and [[multiple inheritance]]<ref>{{cite web|url=https://stat.ethz.ch/R-manual/R-devel/library/base/html/UseMethod.html|title=Class Methods|access-date=2024-04-25}}</ref> In the example, <code>summary</code> is a [[generic function]] that dispatches to different methods depending on whether its [[Argument of a function|argument]] is a numeric [[Vector (mathematics and physics)|vector]] or a "factor": <syntaxhighlight lang="rout"> > data <- c("a", "b", "c", "a", NA) > summary(data) Length Class Mode 5 character character > summary(as.factor(data)) a b c NA's 2 1 1 1 </syntaxhighlight> === Modeling and plotting === [[File:Plots from lm example.svg|right|thumb|200px|Diagnostic plots from plotting "model" (q.v. "plot.lm()" function). Notice the mathematical notation allowed in labels (lower left plot).]] 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. <syntaxhighlight lang="r"> # Create x and y values x <- 1:6 y <- x^2 # Linear regression model y = A + B * x model <- lm(y ~ x) # Display an in-depth summary of the model summary(model) # Create a 2 by 2 layout for figures par(mfrow = c(2, 2)) # Output diagnostic plots of the model plot(model) </syntaxhighlight> Output: <syntaxhighlight lang="rout"> 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 </syntaxhighlight> === Mandelbrot set === [[File:Mandelbrot Creation Animation.gif|thumb|200px|"Mandelbrot.gif" graphic created in R. (Note: Colors differ from actual output.)]] This [[Mandelbrot set]] example highlights the use of [[complex numbers]]. It models the first 20 [[iteration]]s of the [[equation]] <code>z = z<sup>2</sup> + c</code>, where <code>c</code> represents different [[Complex number|complex constants]]. Install the package that provides the <code>write.gif()</code> function beforehand: <syntaxhighlight lang="r"> install.packages("caTools") </syntaxhighlight> R Source code: <syntaxhighlight lang="r"> library(caTools) jet.colors <- colorRampPalette( c("green", "pink", "#007FFF", "cyan", "#7FFF7F", "white", "#FF7F00", "red", "#7F0000")) dx <- 1500 # define width dy <- 1400 # define height C <- complex( real = rep(seq(-2.2, 1.0, length.out = dx), each = dy), imag = rep(seq(-1.2, 1.2, length.out = dy), times = dx) ) # reshape as matrix of complex numbers C <- matrix(C, dy, dx) # initialize output 3D array X <- array(0, c(dy, dx, 20)) Z <- 0 # loop with 20 iterations for (k in 1:20) { # the central difference equation Z <- Z^2 + C # capture the results X[, , k] <- exp(-abs(Z)) } write.gif( X, "Mandelbrot.gif", col = jet.colors, delay = 100) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)