# Tagged: R Language

## Binomial Random number Generation in R

We will learn here how to generate Bernoulli or Binomial distribution in R with the example of a flip of a coin. This tutorial is based on how to generate random numbers according to different statistical distributions in R. Our focus is on binomial random number generation in R.

We know that in Bernoulli distribution, either something will happen or not such as coin flip has to outcomes head or tail (either head will occur or head will not occur i.e. tail will occur). For an unbiased coin, there will be a 50% chance that the head or tail will occur in the long run. To generate a random number that is binomial in R, use rbinom(n, size, prob) command.

rbinom(n, size, prob) #command has three parameters, namey

where
‘n’ is the number of observations
‘size’ is the number of trials (it may be zero or more)
‘prob’ is the probability of success on each trial for example 1/2

Some Examples

• One coin is tossed 10 times with probability of success=0.5
coin will be fair (unbiased coin as p=1/2)
rbinom(n=10, size=1, prob=1/2)
OUTPUT: 1 1 0 0 1 1 1 1 0 1
• Two coins are tossed 10 times with probability of success=0.5
• rbinom(n=10, size=2, prob=1/2)
OUTPUT: 2 1 2 1 2 0 1 0 0 1
• One coin is tossed one hundred thousand times with probability of success=0.5
rbinom(n=100,000, size=1, prob=1/2)
• store simulation results in $x$ vector
x<- rbinom(n=100,000, size=5, prob=1/2)
count 1’s in x vector
sum(x)
find the frequency distribution
table(x)
creates a frequency distribution table with frequency
t = (table(x)/n *100)}
plot frequency distribution table
plot(table(x),ylab = "Probability",main = "size=5,prob=0.5")

View Video tutorial on rbinom command

## Using R as Calculator

In the Windows Operating system, The R installer will have created an icon for R on the 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 the console is the prompt symbol. In this tutorial, we will use the R language as a calculator (we will be Using R as a 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 the keyboard to execute the command. Some examples using R as a 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 work fine for complex numbers.

Upon pressing the enter key, the result of the 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 advanced 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 makes 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 subtracts 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 an infinity (Inf)
> 0/0   #Not a number (NaN)
> “one”/2   #Strings or character is divided by a number