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
'+' for addition '-' for subtraction '*' for multiplication '/' for divisionNote 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
> z = 22.9 > r = 6.7 > y = 1.0 + (z/r)^0.38 > y
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
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.---------------------------------------------- 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
> a = 33.5 > b = 50.0 > x = 4.1 > l = 2.3 > > M = (a/(a+b)) * sin(x/l) * cos(x/l) > > M
> z = 22.9 > r = 6.7 > y = sqrt(1.0 + (z/r)^0.38) > y