Statistics Using R Language
Statistics Using R Language
Statistics Using R Language
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
}
Example 1:
new.function <- function(a) {
For (i in 1:a) {
b <- i^3
Print(b)
}
}
#For call the function
new.function(7)
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)
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")
}
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
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