Biostatistics with R

Numbers and mathematics

In R, we can directly assign integer and floating point values to variables without having to declare the data type of the variable. (This is unlike higher languages like C or Java where the data type of the variable should be explicitly declared using syntax like "int a = 10;", "float x = 3.145;"). The mathematical operations can be performed with the help of functions from maths library of R, which is same as the C math library used in the languages like C, C++, java and python.
We have already performed a simple operation "c = a+b" in the previous section.

For example, we can perform the operation $$ c = \frac{(a + b)}{(a \times b)}$$ by assigning values directly at declaration

> a = 7.5 > b = 6 > c = (a+b)/(a*b) > c
[1] 0.3
The following four letters are allotted for the four basic numerical operations:

      '+'  for addition

      '-'  for subtraction

      '*'  for multiplication

      '/'  for division


Note that the brackets are used to set the operator precedence and clarity, as followed in any other computer language. For example, "a+b/c" adds the result of "b/c" to "a", while "(a+b)/c" divides the sum of "a+b" by "c". They end up with different values.

Next level of important operation needed is to "raise to the power, like "$10^2 = 100$". The symbol "^" (called the "caret symbol") on keyboad is used for this. The R command "10^2" raises 10 to the power of 2 and prints "100" as the answer.
> 10^2
[1] 100
In another example, we apply the formula $$y = 1.0 + {\large \left (\dfrac{z}{r}\right )}^{0.38} $$ as,

> z = 22.9 > r = 6.7 > y = 1.0 + (z/r)^0.38 > y
[1] 2.595251
where the symbol "^" reresents "to the power". Thus, "(z/r)^0.38" "divides z by r and raises the resulting number to the power 0.38".

Note the explicit usage of brackets for grouping the terms to implement operator precedence and to remove ambiguity.

Math library functions

In order to carry out certain standard mathematical operations, R has internal library functions. In programming languages, a function is a collection of code statements that can perform a task. A function takes one or more input parameter values and should give a single output object as result.


For example, if we want to find the natural logarithm of a variable x, we can call the function called log as follows:
> x = 42.5 > y = log(x) > y
[1] 3.749504

In the above R codelines, variable x is assigned a value of 42.5. The function call y = log(x) passes the value of x to the function and the value returned by the function is assigned to the variable y. When we print y, we get 3.749504, which is the natural logarithm of 42.5.

Here, "log" is the function name. It is followed by a pair of brackets inside which a singe variable name of a set of comma separated names are given.

Some of the functions will take additional parameters for specific actions. For example, the function y = round(x, digits=3) will round up the value of "x" to 3 digits. Here the parameter "digits" is defined by the function, and should be strictly used as such.

A list of important math library functions in R are given below. many more will be introduced in the coming sections. Each function will have a name that refelects what it is used for, and one or more input parameters.

----------------------------------------------
Function name            Description
----------------------------------------------
abs(x)	                  absolute value
sqrt(x)                   square root
ceiling(x)                ceiling(3.475) is 4
floor(x)                  floor(3.475) is 3
trunc(x)                  trunc(5.99) is 5
round(x, digits=n)        round(3.475, digits=2) is 3.48
signif(x, digits=n)       signif(3.475, digits=2) is 3.5
cos(x),sin(x),tan(x)      Triginomteric sine, cosine and tan functions
acos(x),asin(x),atan(x)   arcsine, arccosine and arctangent functions
log(x)                     natural logarithm. log(10) is 2.302585
log10(x)                  common logarithm to the base 10. log10(100) is 2
log2(x)                   logarithm to the base of 2. log2(32) is 5.
exp(x) 	                   exponential of x. Thus, exp(1) is 2.718282
-----------------------------------------------

Let us now translate some mathematical expressions into R code lines:

Example 1 : $$ Q = A~log(m^2 + n^2) $$
> A = 120.5 > m = 2.0 > n = 3.2 > > Q = A * log(m^2 + n^2) > > Q
[1] 320.0546


Example 2 : $$ M = \left(\frac{a}{a+b}\right) sin\left (\frac{x}{l}\right )~cos\left (\frac{x}{l}\right ) $$
 
> a = 33.5 > b = 50.0 > x = 4.1 > l = 2.3 > > M = (a/(a+b)) * sin(x/l) * cos(x/l) > > M
[1] -0.08245964


Example 3 : $$ M = \sqrt{1.0 + {\large \left(\frac{z}{r}\right)}^{0.38}} $$
> z = 22.9 > r = 6.7 > y = sqrt(1.0 + (z/r)^0.38) > y
[1] 1.610978