By: Shakthi Kannan
Figure 2: Shades of blue
The image in Figure 2 is the output from the above example. R has a number of built-in numeric functions. A few examples (square root, absolute value, floor, ceiling, truncate, cosine, exponent) with their respective outputs are shown below:
> sqrt(2) [1] 1.414214
> abs(-3) [1] 3
> floor(5.67) [1] 5
> ceiling(5.67) [1] 6
> trunc(4.32) [1] 4
> cos(0) [1] 1
> exp(1) [1] 2.718282
There are also predefined functions (to upper case, to lower case, grep, string split) which operate on characters that you can use as follows:
> toupper(‘project’) [1] “PROJECT”
> tolower(‘LOWER’) [1] “lower”
> grep(‘l’, ‘lower’) [1] 1
> grep(‘l’, ‘upper’)
integer(0)
> strsplit(“0,Item,Quantity,GST”, “,”) [[1]] [1] “0” “Item” “Quantity” “GST”
Since R is designed for statistical computing, there are also built-in statistical functions (sum, minimum, maximum, range, mean, median) available. A few examples are shown below:
> sum(1, 2, 3) [1] 6
> min(1, 2, 3) [1] 1
> max(1, 2, 3) [1] 3
> range(1, 2, 3) [1] 1 3
> x <- c(1, 2, 3)
> mean(x) [1] 2
> median(x) [1] 2
You can load a library into the R runtime environment using the library function. We will now import the Lattice library in R, which is useful for visualising data:
> library(lattice) >
There also exists a ‘citation’ function that gives you information on how to cite R or its packages when mentioning it in publications. The output for the same is shown below for reference:
> citation()
To cite R in publications use:
R Core Team (2021). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/.
A BibTeX entry for LaTeX users is @Manual{, title = {R: A Language and Environment for Statistical Computing}, author = {{R Core Team}}, organization = {R Foundation for Statistical Computing}, address = {Vienna, Austria}, year = {2021}, url = {https://www.R-project.org/}, } We have invested a lot of time and effort in creating R, please cite it when using it for data analysis. See also ‘citation(“pkgname”)’ for citing R packages.
History
R is an alternate implementation of the S programming language. S is a statistical programming language created by John Chambers in 1976 at Bell Laboratories (previously AT&T). Rick Becker and Allan Wilks of Bell Laboratories have also worked on the initial releases of S. The S programming language is dynamically and strongly typed, and supports both the imperative and object-oriented styles of programming. Most of the S code actually runs without alterations on R.
In 1991, Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, wrote an alternative implementation to the S programming language, which was promoted as the R programming language in 1993.
The R project was officially released in 1995 as Free/Libre and Open Source Software (FOSS) and is now maintained by the R core team.
The ‘R Foundation for Statistical Computing’ or the ‘R Foundation’ was created by the R core team to facilitate the development of the R programming language, and its tools and ecosystem. It also offers support for all users, developers and organisations using R in the community and for commercial purposes. It is responsible for the copyright of the R software and documentation. The foundation also conducts meetings and conferences regularly, and its annual conference is called useR!.
In the next article in this series, we will go over the syntax and semantics of the R programming language.
The author is a free software enthusiast who blogs at shakthimaan.com.