logo

An Advance Calculator

R Can be think as an advance calculator which can handle all basic calculations:

> 2+6
[1] 8
> 3-1
[1] 2
> 2*6
[1] 12
> 18/2
[1] 9
> 2^4
[1] 16

Arithmetic operators In R

+ Addition
- Subtraction
* Multiplication
/ Division
^ to-the-power

There are some pre-defined functions in R that can be used to do some other arithmetic operations. For example:

> # will give the sum of all the numbers
> sum(2,3,4,5,6,8) 
[1] 28
> # will give the value of 5!
> factorial(5)  
[1] 120
> # will give the square root of 16
> sqrt(16)      
[1] 4
> # will give the absolute value of the number
> abs(-5)       
[1] 5
> # will calculate exponential of 5
> exp(5)        
[1] 148.4132
> # log of 2 to the base e
> log(2)        
[1] 0.6931472
> # log of 100 to the base 10
> log10(100)    
[1] 2
> # will round off the number/vector to given number of decimal places.
> round(3.2666666,2)    
[1] 3.27
> # truncates the decimal part of a number/vector.
> trunc(3.266666)   
[1] 3
> # the vector of small letters
> letters      
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n"
[15] "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
> # the vector of CAPITAL letters.
> LETTERS      
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N"
[15] "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
> # Selecting the first 4 small letters.
> letters[1:4] 
[1] "a" "b" "c" "d"
> # Selecting the first four capital letters.
> LETTERS[1:4] 
[1] "A" "B" "C" "D"