S Lazy-H
  • Home
  • About
  • Posts
  • Contact
  • Slide Rules
  • A Biker’s Tale

Fun R Functions Part 2

R Programming
mathematics
Author

Sam Hutchins

Published

October 21, 2023

Following along with the first post, here are a few more functions in R Core Team (2023), in this case the pracma package.

I’m going to just pick and choose some functions, so let’s start with the factors() function to compute the prime factors of a number. This can determine factors up to \(2^{53 -1}\) before it errors.

factors(2^52 +1)
[1]        17    858001 308761441

If you wanted to know all the primes up to a particular number, primes will determine those,

primes(50)
 [1]  2  3  5  7 11 13 17 19 23 29 31 37 41 43 47

I may have mentioned the hadamard function in another post, but it is handy for generating a matrix for many purposes.

hadamard(4)
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]    1   -1    1   -1
[3,]    1    1   -1   -1
[4,]    1   -1   -1    1

The haversine() function will compute the arc distance between two points (on Earth, for example) along a great circle path.

a <- '30 12 10N, 100 22 14W'
b <- '41 58 43N, 87 54 17W'
c <- haversine(a,b)
fprintf("Distance: %.2f kilometers.",c)
Distance: 1719.31 kilometers.

The hypot() function determines the hypotensue of a right triangle. The well-known right angle for making a square corner in carpentry.

hypot(3,4)
[1] 5

Integrals are always handy. The basic format is,

\[\int_{_{x1}}^{^{x2}} a -b \ x^2 \quad dx \ dy\]

As an example, if you wished to determine the volume of a building with a 40 ft domed roof (profile \(0.03x^2\))1 with dimensions of 200 ft by 40 ft,

\[\int_{_{-100}}^{^{100}} \ \int_{_{-20}}^{^{20}} 40 - 0.3x^2 + y \quad dx \ dy\]

Using the integrals2() function, we do the inner product first, then the outer,

a <- 40; b <- .03; xmin <- -20
xmax <- 20; ymin <- -100; ymax <- 100
fun = function(x,y) { a - b * x^2 + y }
ans = integral2(fun, xmin, xmax, ymin, ymax)
print(paste("Cubic feet:", ans$Q))
[1] "Cubic feet: 288000"

There are other elements to the ans vector, such as the projected error, but we are only interested in the value itself Q).

Related to factors() used above, is the isprime(), which determines if a positive integer is actually prime. Roots are also handy for many things. To determine the root of a number is fairly simple. For another radix than 2 (i.e., cube root), the nthroot() function makes it simple for scripts, and looks like this for cube root,

\[X = \sqrt[3]{27}\] and in R,

nthroot(27,3)
[1] 3

If you wished to know all permutations (different from combinations) of several numbers, the perms() function places them in a nice matrix format,

perms(c(4,6,1))
     [,1] [,2] [,3]
[1,]    1    6    4
[2,]    1    4    6
[3,]    6    1    4
[4,]    6    4    1
[5,]    4    6    1
[6,]    4    1    6

For some eye-candy, if you had a need to plot two different sets of data, with different scales, the plotxy() functions is simple to use,

x <- seq(0, 20, by = 0.01)
y1 <- 200*exp(-0.05*x)*sin(x)
y2 <- 0.8*exp(-0.5*x)*sin(10*x)
plotyy(x, y1, x, y2, main = "Two-ordinates Plot")

Using in this case abstract angles, if you wanted to plot the radiation angle of a horizontal antenna element at ground level, the polar() function could be used,

t <- deg2rad(seq(0, 180, by = 2))
polar(t, sin(1*t), col = "red")

The rand() series of functions can create random matrices of n x m. Using randi(12,4,6), we see this,

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    8    4    8    6    4    8
[2,]    1    9    2    7    8    1
[3,]    8    6    8    1    5    4
[4,]    1    7    2   10    4   11

There are many functions to find unknown values for polynomials, such as echelon() in the previous post. The reduced row echelon format (RREF) is easily done using rref(),

\[ rref \begin{bmatrix}4& 1& -3\\ -1& 7& 11\end{bmatrix} \quad \Rrightarrow \quad \begin{bmatrix}1& 0& -1.1035\\0& 1& 1.4138\end{bmatrix} \]

And that’s just a fraction of the functions in pracma. As can be seen when using R, there are “many ways to skin a cat.”

Have a great day! And remember, if you don’t know the Lord Jesus as your personal Lord and Savior, there’s no time like the present. Don’t wait until it’s too late!


References

R Core Team. 2023. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Footnotes

  1. Curve fitting can be used to establish the profile. The polyfit function with x,y point sets for input can derive the variable↩︎

© S Lazy-H 2019 -