Muthulakshmi M: Software Technical Trainer IBM
Muthulakshmi M: Software Technical Trainer IBM
Muthulakshmi M: Software Technical Trainer IBM
•This programming language name is taken from the name of both the
developers. The first project was considered in 1992.
•The initial version was released in 1995, and in 2000, a stable beta
version was released.
History of R
Features of R
❖ R supports Object-oriented as well as Procedural programming.
❖ It provides an environment for statistical computation and software
development.
❖ Provides extensive packages & libraries.
❖ R has a wonderful community for people to share and learn from
experts.
❖ Numerous data sources to connect.
❖ includes all topics of R such as introduction, features, installation,
rstudio ide, variables, datatypes, operators, if statement, vector, data
handing, graphics, statistical modelling, etc.
Features of R
❖ Statical Inference
❖ Data Analysis
❖ Machine Learning Algorithm
❖ Data Visualization.
IDEs for R Language
❖ R Studio.
❖ Visual Studio for R
❖ Eclipse for R
R Language- Statistical Algorithm
⮚ print(“data Visualization
⮚Print(“Enter the value”)
Variables
EXAMPLE:
A=readline()
A=as.integer(A)
print(A)
R
EXAMPLE:
var = readline(prompt = "Enter any number : ");
var = as.integer(var);
print(var)
Taking multiple input in R
Syntax: variable<-c(values)
Example :
#vectors
x=c(2,4,6,8,10)
print(x)
.
Creating Vector
Syntax: variable<-c(values)
C()- Function is used to create a vector.
R<-c(1:5)
Language<-c(“c”, “c++”, “java”, “python”, “r”)
Number<-c(1,2,3,4,5)
Number<-c(c=1,c++=2,java=3,python=4,r=5)
Number<-c (“c”=1, “c++”=2, “java”=3, “python”=4, “r”=5)
1byte of memory space will occupied by Vector.
We can create Vector in another way.
The method scan() is used in create a vector at Run time.
example<-scan()
scan() function allow as to give input in output area
Vectors are Homogeneous
.
Array
•Create
•Access
•Modify all we can do in array
Syntax:
array_name <- array(data, dim= (row_size, column_size, matrices, di
m_names))
.
Array
The data is the first argument in the array() function. It is an input vector which is
given to the array.
matrices
In R, the array consists of multi-dimensional matrices.
row_size
This parameter defines the number of row elements which an array can store.
column_size
This parameter defines the number of columns elements which an array can store.
dim_names
This parameter is used to change the default names of rows and columns.
Array
#Creating two vectors of different lengths
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
#Taking these vectors as input to the array
res <- array(c(vec1,vec2),dim=c(3,3,2))
print(res)
Matrix
123
456
o/p: 4 6 8
10 12 14
Factors
⮚Factors are the data objects which are used to categorize the
data and store it as levels.
⮚They are useful for storing categorical data. They can store
both strings and integers.
⮚They are useful to categorize unique values in columns like
“TRUE” or “FALSE”, or “MALE” or “FEMALE”, etc.. They
are useful in data analysis for statistical modeling.
Factors
EXAMPLE:
fac = factor(c("Male", "Female", "Male",
"Male", "Female", "Male", "Female"))
print(fac)
.
Factors
EXAMPLE:
data <- c("Nishka","Gunjan","Shubham","Arpita","Arpita","Sumit","Gunjan","Shubham
")
# Creating the factors
factor_data<- factor(data)
print(factor_data)
# Apply the factor function with the required order of the level.
new_order_factor<- factor(factor_data,levels = c("Gunjan","Nishka","Arpita","Shubham
","Sumit"))
print(new_order_factor)
Factors
EXAMPLE:
gen_factor<- gl(3,5,labels=c("BCA","MCA","B.Tech"))
gen_factor
Only element from same data type.
Program<-c(1, “c++”, 3, “python”, “r”)
Vector take default data type character.
Vectors are Homogeneous
o/p:
a b c
1 2 20 22
2 3 30 33
Extracting
Program<-c(1:4)
Program[5]
Program[c(1,5,9)]
Program[-1], Program[-5]
Program[-length(program)]
Program[seq(2, length(Program),2)]-- find even number.
Program[seq(3,length(program),3]-- odd number.
sum(Program<10)
Program<10
sum(Program[Program<15])
sum(Program[Program%%15==0])
cor(x,y) – finding correlation
cumsum(Program) – cumulative summation.
cumprod(Program) – cumulative Product.
fivenum(Program)
Sorting
sort(Program)
rev(sort(Program))
sum(rev(sort(Program))[1:3])
which(Program==max(Program))
Program[which(Program==max(Program))]
which(Program==min(Program))
Program[which(Program==min(Program))]
rank(Program)
order(Program)
quantile(Program)
If condition
⮚For loop : execute statement multiple times and check the condition
at the end
⮚While loop : execute code till condition is satisfied .
⮚Repeat loop : Repeats the statement multiple times .
For loop
⮚Syntax:
for (variable in vector )
{
condition
}
Example:
For(i in 1:5)
{
Print(i^2)
}
For loop
X<- c(1,2,3,4)
For(i in x)
{
Print(i,i^2)
}
Example 2:
A<- LETERS(1:10)
For i in a
{
Print(i)
}
While loop
⮚Syntax:
while (test_expression) {
statement
}
While loop
v <- c("Hello","while loop","example")
cnt <- 2
while (cnt < 7) {
print(v)
cntcnt = cnt + 1
}}
Repeat loop
Syntax:
repeat {
commands
if(condition) {
break
}
}
Repeat loop
v <- c("Hello","repeat","loop")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5) {
break
}
}
Function in R
•A set of statements which are organized together to perform a specific
task is known as a function. R provides a series of in-built functions,
and it allows the user to create their own functions. Functions are used
to perform tasks in the modular approach.
•Functions are used to avoid repeating the same task and to reduce
complexity. To understand and maintain our code, we logically break it
into smaller parts using the function. A function should be
•Written to carry out a specified task.
•May or may not have arguments
•Contain a body in which our code is written.
•May or may not return one or more output values.
Function in R
Function Name
The function name is the actual name of the function. In R, the function is stored as an
object with its name.
Arguments
In R, an argument is a placeholder. In function, arguments are optional means a function
may or may not contain arguments, and these arguments can have default values also. We
pass a value to the argument when a function is invoked.
Function Body
The function body contains a set of statements which defines what the function does.
Return value
It is the last expression in the function body which is to be evaluated.
Function in R
Function Name
The function name is the actual name of the function. In R, the function is stored as an
object with its name.
Arguments
In R, an argument is a placeholder. In function, arguments are optional means a function
may or may not contain arguments, and these arguments can have default values also. We
pass a value to the argument when a function is invoked.
Function Body
The function body contains a set of statements which defines what the function does.
Return value
It is the last expression in the function body which is to be evaluated.
Function in R
•Afunc<-function(a)
{
For (i in 1:a ) //1,2,3
{
B<-i*2
Print (b)
}
}
afun(3)
Class in R