You are on page 1of 5

STATISTICS USING R LANGUAGE

R is a programming language or preferably, a programming package used for data


visualization, data analytics, business intelligence, statistical analysis, business analysis, and
research and much more. Because of its expressive sentence structure and simple to-utilize
interface, it has gained immense prominence in the latest past.

The best part is R is open source, free and runs on all platforms!
If you are enthusiastic about opting for a career in Data Science, if you want to pursue
machine learning or business intelligence, my friend, the R Ecosystem is taking on top of it!
Do not, I repeat, do not miss out on your chance to knowing R. At present, it is being used by
today’s greatest tech corps. like Twitter, Microsoft and other companies like Ford, New York
Times, etc.
R is mainly used for academic purposes and statistical analysis.

Functions in R Programming
A function, in a programming environment, is a set of instructions. Along with a large
number of inbuilt functions, R provides a platform to create user-built functions.
A general approach to a function is to use the argument part as inputs, feed the body part and
finally return an output.

To elaborate further,
The different parts of a function are −
•       Function Name − This is the actual name of the function which gets stored in the R
environment with the object name. 
•       Arguments − An argument works as a placeholder. When a function is called, the value
of the argument is passed through it. Arguments are optional; that is, a function may contain
no arguments. Also, arguments can have default values.
•       Function Body − The function body holds what the function has to perform including
all the operations and control statements. 
•       Return Value – The last expression evaluated while performing a function is the return
value.
The basic syntax of an R function definition is as follows –
Syntax:
function (argument list)
{
Function body
}

BUILT-IN FUNTIONS IN R LANGUAGE


Some in-built functions are seq(), mean(), median(), sum(), paste() etc. which are directly
invoked by the user-built programs.

 USER DEFINED FUNCTIONS IN R LANGUAGE

Example 1:
new.function <- function(a) {
For (i in 1:a) {
b <- i^3
Print(b)
}
}
#For call the function
new.function(7)

Example 2: CALLING A FUNCTION WITHOUT AN ARGUMENT


# Create a function without an argument.
new.function <- function() { 
for(i in 1:7) { 
  print(i^3) 
  } 
     }  
 # Call the function without an argument.
new.function()

Example 3: Calling a Function with Argument 


(by position and by name)
new.function <- function(a,b,c) { 
result <- a+b-c 
print(result) 
}  
# Call the function by position of arguments. 
new.function(7,2,18) 
# Call the function by names of the arguments. 
new.function(a=12,b=2,c=3) 

Now let us discuss - R in Statistics.


Statistical Functions in R 
 mean(x)- Mean of the numbers in vector x.
 median(x)-Median of the numbers in vector x.
 var(x)-Estimated variance of the population from which the numbers in vector x are
sampled.
 sd(x)-Estimated standard deviation of the population from which the numbers in
vector x are sampled.
 scale(x)-Standard scores (z-scores) for the numbers in vector x.
 
DATA RESHAPING IN R
cbind() Function-Used to join multiple vectors(data structure in R) to create a data frame.
rbind() Function- Used to merge two data frames.

CODE:
city <- c(“Chandigarh ",“Meerut",“Dehradun ",“delhi") 
state <- c(“Punjab",“UP",“Uttrakhand",“delhi") 
zipcode <- c(33602,98104,06161,80294) 
# Combine above three vectors into one data frame.
 addresses <- cbind(city,state,zipcode) 
# Print the data frame.
 print(addresses)

Create another data frame with similar columns in variable new.address.


Combine rows form both the data frames.
 all.addresses <- rbind(addresses,new.address)

DECISION MAKING IN R

1)    if statement - A Boolean expression focusing on a particular condition enforced within
the statement, followed by other statements of the function.

EXAMPLE:
x <- 50
 if(is.numeric(x))
 {
 print("X is an numeric") 
}

2)    If-else statement- A Boolean expression focusing on a particular condition enforced


within the if statement, followed by the else statement given the Boolean expression for the if
statement is false.

EXAMPLE:
x <- c("what","is","truth") 
if(“Truth" %in% x) { 
print("Truth is found")
 } else {
 print("Truth is not found") 
}

3)    switch statement- Given a valid set of values, the switch statement allows to check for
equality of a variable.

EXAMPLE:
x <- switch( 2, 
"first", "second", "third", "fourth" )
print(x)

R-LOOPS

1)    repeat loop- Performs a sequential set of statements in circle to shorten the code that
deals with the loop variable
SYNTAX:
Repeat
 {
commands
if(condition) { 
Break
 } 
}
EXAMPLE:
v <- c("HELLO",“UPES")
 cnt <- 3 
Repeat
 { 
print(v)
 cnt <- cnt+1
 if(cnt > 8) 
{
 break
 }
 }

2)    While loop- It executes the statement or a set of statements to repeat while checking for
a given condition to be valid. The condition is tested before the execution of the loop body.
SYNTAX:
While (test expression)
{
statement 
}
EXAMPLE:
v <- c("Hello",”Everyone")
 cnt <- 2 
while (cnt < 8) 
{
 print(v)
 cnt = cnt + 1
 }

3)    for loop- It executes the statement or a set of statements to repeat while checking for a
given condition to be valid. The condition is tested after the complete execution of the loop
body
SYNTAX:
for (value in vector)
 {
statements 
}
EXAMPLE:
v <- LETTERS[2:6]
 for ( i in v) 
{
 print(i)
 }

In the next article, we shall discuss the different data structures of R, graphs in R and plotting
on R.
If you wish to know more about statistics using R, python or any update, you can check out
our eBooks(30+) on amazon! 
This article is written by @TanishqaRawlani

For more such articles:

http://thedatamonk.com/statistics/
http://thedatamonk.com/10-questions-10-minutes-sql-r-python-3-100/

http://thedatamonk.com/python-r/

KEEP LEARNING!
THE DATA MONK

You might also like