Base R Essentials - Part 4

Some essentials Base R functions still important to know in the Tidyverse ecosystem (Mathematical functions)

R
Base
Author

Abdoul ISSA BIDA

Published

March 5, 2023

Welcome to this final part of this series. I will list out in this last one the essential mathematical functions.
If some of the functions from Part III can be included in this part, here I focus more on the functions used for statistical computing.

Functions Tasks / Examples
abs(x) computes the absolute value of elements of the vector x.
sin(x),cos(x), tan(x) respectively compute the cosine, sine and tagent of the numeric or complex vectors x1.
Ex: sin(pi), cos(c(pi/4, pi/2))
acos(x), asin(x), atan(x) respectively compute the arc-cosine, arc-sine and arc-tangent of the numeric or complex vectors x.
cospi(x), sinpi(x), tanpi(pi) are accurate for x values which are multiples of a half. cospi(1) is equivalent to cos(pi).
max(x)2 returns the maximum of elements of the vector x.
min(x) returns the minimum of elements of the vector x.
rle(x) returns the lengths and values of runs of equal values in a vector. The function can be useful when you want to determine for example the number of consecutive success of a team in their scorecards.
range(x) returns a vector containing the minimum and maximum of the vector x. range(x) is equivalent to c(min(x), max(x)).
Ex: range(c(7,1,2,8,10,5)) returns c(1,10).
sum(x) returns the sum of elements of the vector x.
prod(x) returns the product of elements of the vector x.
diff(x) returns suitable lagged and iterated differences.
Ex: diff(c(4,5,1,9,2)) returns c(1,-4,8,-7).
mean(x) returns the arithmetic mean of elements of the vector x.
Ex:diff(c(4,5,1,9,2)) returns 4.2.
weighted.mean(x,w) returns the weighted mean of elements of the vector x by weights vector w.
Ex: weighted.mean(c(1,2,3), c(2,1,1)) returns 1.75 for (1*2 + 2*1 + 3*1)/4.
median(x) returns the sample median of elements of the vector x.
var(x, y), cov(x, y), cor(x, y) respectively compute the variance, the covariance and the correlation of x and y vectors3. For var and cov functions, you can just provide a vector and it will compute (with n-1 in denominator) the vector variance/covariance of the vector.
round(x,n) rounds the values in its first argument to the specified number of decimal places.
Ex: round(2.138, 2) returns 2.14.
ceiling(x)/floor(x) takes a single numeric argument x and returns a numeric vector containing the smallest/largest integers not less/greater than the corresponding elements of x.
Ex: ceiling(2.138) returns 3.
floor(2.138) returns 2.
trunc(x) works approximately like floor(x) and ceiling(x), except that for the numeric argument x, it returns the numeric vector formed by truncating the values in x toward 0.
To understand, floor(1.65) & trunc(1.65) return 1. But floor(-1.65) returns -24 when trunc(-1.65) returns -15
union(x,y) returns the union of the two vectors.
Ex: union(c(1,4,5), c(1, 4, 8)) returns c(1,4,5,8).
intersect(x,y) returns the intersection of the two vectors.
Ex: intersect(c(1,4,5), c(1, 4, 8)) returns c(1,4).
setdiff(x,y) returns the elements of x which are not in y.
Ex:
setdiff(c(1,4,5), c(1, 4, 8)) returns c(5).

We are coming to the end of this series. As I explained at the beginning, the goal is note to list all base R functions available, but those I have found useful in my R learning path.

Personally, I don’t think it is a primordial need to know about these functions. They come naturally in my background because they help me quickly solve programming problems that I encounter in my daily life.

Many folks, including me, come to R, for quick data wrangling & visualization, via tidyverse ecosystem. Hadley and Posit (ex RStudio) Team that develop and maintain all these packages have done an amazing job of making these packages syntactically pleasing, well documented, easy to understand and remember.

That pedagogy gives us the will to learn more to build blog, packages and web applications via the Shiny framework. And as we learn more and more, we start to dive deeper and deeper into the basics of R language.

Assert that someone whose work mostly relies on tidyverse, don’t know R is pedagogically nonsense. It is a negation of the mutations that affect every programming language and a separation of the communities that are newly adopting a vision of coding.
The question shouldn’t be about to determine who is a “real” R developer, but about engaging more and more people from different backgrounds to code with R. Their visions will enrich the language and their contributions will make R more in tune with its time.

Footnotes

  1. For all these methods, angles are in radians.↩︎

  2. Most of these functions have a parameter na.rm=FALSE (non available removed) to remove the missing data before the computing.↩︎

  3. If x and y are matrices then the covariances (or correlations) between the columns of x and the columns of y are computed.↩︎

  4. the largest integer not great than -1.65↩︎

  5. the integer formed by truncating the value of -1.65 toward 0.↩︎

Citation

BibTeX citation:
@online{issabida2023,
  author = {Abdoul ISSA BIDA},
  title = {Base {R} {Essentials} - {Part} 4},
  date = {2023-03-05},
  url = {https://www.abdoulblog.com},
  langid = {en}
}
For attribution, please cite this work as:
Abdoul ISSA BIDA. 2023. “Base R Essentials - Part 4.” March 5, 2023. https://www.abdoulblog.com.