# Using R as Calculator

In Windows Operating system, The R installer will have created an icon for R on desktop and a Start Menu item. Double click the R icon to start the R Program; R will open the console, to type the R commands.

The greater than sing (>) in console is the prompt symbol. In this tutorial we will use R language as calculator (we will be Using R as Calculator for mathematical expressions), by typing some simple mathematical expressions at the prompt (>). Anything that can be computed on a pocket calculator can also be computed at the R prompt. After entering the expression on prompt, you have to press the Enter key from keyboard to execute the command. Some examples using R as calculator are as follows

> 1 + 2   #add two or more numbers
> 1 – 2   #abstracts two or more numbers
> 1 * 2   #multiply two or more numbers
> 1 / 2   #divides two more more numbers
> 1%/ %2   #gives the integer part of the quotient
> 2 ^ 1   #gives exponentiation
> 31 %% 7   #gives the remainder after division

These operators also works fine for complex numbers.

Upon pressing the enter key, the result of expression will appear, prefixed by a number in square bracket:
> 1 + 2
[1] 54

The [1] indicates that this is the first result from the command.

Some advance calculations that are available in scientific calculators can also be easily done in R for example

> sqrt(5)   #Square Root of a number
> log(10)   #Natural log of a number
> sin(45)   #Trignometric function (sin function)
> pi   #pi value 3.141593
> exp(2)   #Antilog, e raised to a power
> log10(5)   #Log of a number base 10
> factorial(5)   #Factorial of a number e.g 5!
> abs(1/-2)   #Absolute values of a number
> 2*pi/360   #Number of radian in one Babylonian degree of a circle

Remember R prints all very large or very small numbers in scientific notation.

R language also make use of parentheses for grouping operations to follow the rules for order of operations. for example

> 1-2/3   #It first computes 2/3 and then subtract it from 1
> (1-2)/3   #It first computes (1-2) and then divide it by 3

R recognizes certain goofs, like trying to divide by zero, missing values in data etc.

> 1/0   #Undefined, R tells it a infinity (Inf)
> 0/0   #Not a number (NaN)
> “one”/2   #Strings or character is divided by a number

#### Download pdf file: R Language as Calculator 53.52 KB R Language as Calculator

Updated: Apr 13, 2014 — 1:50 pm