R-Programming Record - Odd Sem 21-22
R-Programming Record - Odd Sem 21-22
SNSRAJALAKSHMI
COLLEGE OFARTSANDSCIENCE
(AUTONOMOUS)
Accredited by NAAC (Cycle – III) with ‘A+’
Grade(Recognised by UGC, Approved by AICTE,
New DelhiandAffiliated toBharathiarUniversity)
Coimbatore –641049.
Reg No:
byMr./Ms.
1.
2.
INTERNALEXAMINER EXTERNALEXAMINER
TABLEOFCONTENTS
IT APPLICATIONS (R-PROGRAMMING)
Sl. PAGE
DATE TITLE SIGNATURE
NO NO
1 1. Using Class function check the type of data contained in a
variable.
2. Usingncharfunctions find the length of a character.
2 Construct a basic data. Frame,
1. Assign the column names and row names of a data frame.
2. Using head function print out the first few rows.
3. Access entire row while not specifying any column.
4. Access entire column while not specifying any row.
5. Create a set of indicators variable using model matrix.
3 Create an element list - Single, Two, Three, Empty list.
Access an individual element of a list and Append elements
to a list.
4 Create matrix Using R function.
5 Create Arrays Using R function.
6 Read data from a csv file and database
ALGORITHM:
CODING:
OUTPUT:
[1] 1 2 5 3 4 0 -1 -3
[1] "double"
[1] "Red" "Green" "White"
[1] "character"
[1] TRUETRUETRUE FALSE TRUE FALSE
[1] "logical"
CODING:
OUTPUT:
____________________________________________________________________
AIM:
ALGORITHM:
CODING:
1. CREATING DATAFRAME FROM GIVEN 4 VECTORS.
# creating a vector with some value
id = c(1, 2, 3)
# creating another vector with some value
name = c("karthik" , "nikhil" , "sravan")
# creating another vector with some value
branch = c("IT" , "CSE" , "IT")
# creating another vector with some value.
favourite_subject = c("SE" ,"DAA" , "OS")
# passing the vectors into data.frame() function
# as parameters
df1=data.frame(id, name, branch, favourite_subject)
# printing the data frame.
print(df1)
OUTPUT
OUTPUT:
PROGRAM3 DATE:
_______________________________________________________________________________
AIM:
ALGORITHM:
CODING:
CREATING A LIST
# vector with names
names=c("bobby","pinkey","rohith","gnanu")
# vector with marks
marks=c(78,90,100,100)
# address vector
address=c("kakumanu","hyd","hyd","hyd")
# pass these vectors as inputs to the list
# address vector
student=list(names,marks,address)
print(student)
OUTPUT:
____________________________________________________________________
AIM:
ALGORITHM:
CODING:
OUTPUT:
CODING:
OUTPUT:
_______________________________________________________________________________
AIM:
ALGORITHM:
CODING:
1 DIMENSIONAL ARRAY
m=matrix(1:12,3,4)
print("Original matrix:")
print(m)
a = as.vector(m)
print("1 dimensional array:")
print(a)
OUTPUT:
2 DIMENSIONAL ARRAY
,,2
v = sample(1:5,24,replace = TRUE)
dim(v) = c(3,2,4)
print("3-dimension array:")
print(v)
OUTPUT:
[1] "3-dimension array:"
,,1
[,1] [,2]
[1,] 4 2
[2,] 4 2
[3,] 2 1
,,2
[,1] [,2]
[1,] 4 2
[2,] 2 4
[3,] 2 4
,,3
[,1] [,2]
[1,] 4 2
[2,] 1 5
[3,] 3 2
,,4
[,1] [,2]
[1,] 3 3
[2,] 1 5
[3,] 1 2
PROGRAM6 DATE:
_______________________________________________________________________________
AIM:
ALGORITHM:
CODING:
OUTPUT:
C:/Users/Vanshi/Documents
OUTPUT:
PROGRAM7 DATE:
_______________________________________________________________________________
AIM:
ALGORITHM:
CODING:
Step 1: Using the save() function to save the file in.RData format
OUTPUT:
gfg_data= data.frame(A=c(7,6,2,8,1),
B=c(4,2,9,7,3),
C=c(1,7,2,6,8))
print("Dataframe:->")
print(gfg_data)
save(gfg_data,file="gfg.RData")
load("gfg.RData")
STEP3: USING FILE.INFO() FUNCTION TO GET THE INFORMATION OF A
PARTICULAR FILE.
gfg_data= data.frame(A=c(7,6,2,8,1),
B=c(4,2,9,7,3),
C=c(1,7,2,6,8))
print ("Dataframe:->")
print(gfg_data)
save(gfg_data,file="gfg.RData")
load("gfg.RData")
file.info("gfg.RData")
OUTPUT:
PROGRAM8 DATE:
GGPLOT2.
__________________________________________________________
AIM:
ALGORITHM:
CODING:
#bar plot the vector -- simple plot with no legends and colors
OUTPUT:
BASE GRAPHICS
par(las=1)
barplot(dat$total_bill,
names.arg=dat$time,
col="#AFC0CB",
border=FALSE,
main="Average Bill for Two-Person Meal")
OUTPUT:
PROGRAM9 DATE:
____________________________________________________________________
AIM:
ALGORITHM:
CODING:
ADDING ARGUMENTS IN R
# Function definition
# To check n is divisible by 5 or not
divisbleBy5 <- function(n){
if(n %% 5 == 0)
{
return("number is divisible by 5")
}
else
{
return("number is not divisible by 5")
}
}
# Function call
divisbleBy5(100)
divisbleBy5(4)
divisbleBy5(20.0)
OUTPUT:
# Function definition
# To check a is divisible by b or not
divisible<- function(a, b){
if(a %% b == 0)
{
return(paste(a, "is divisible by", b))
}
else
{
return(paste(a, "is not divisible by", b))
}
}
# Function call
divisible(7, 3)
divisible(36, 6)
divisible(9, 2)
OUTPUT:
ALGORITHM:
CODING:
IF CONDITION
x <- 100
if(x > 10){
print(paste(x, "is greater than 10"))
}
OUTPUT:
IF-ELSE CONDITION
x <- 5
# Check value is less than or greater than 10
if(x > 10){
print(paste(x, "is greater than 10"))
}else{
print(paste(x, "is less than 10"))
}
OUTPUT:
val1 = 6
val2 = 7
val3 = "s"
result = switch(
val3,
"a"= cat("Addition =", val1 + val2),
"d"= cat("Subtraction =", val1 - val2),
"r"= cat("Division = ", val1 / val2),
"s"= cat("Multiplication =", val1 * val2),
"m"= cat("Modulus =", val1 %% val2),
"p"= cat("Power =", val1 ^ val2)
)
print(result)
OUTPUT:
Multiplication = 42NULL
PROGRAM11 DATE:
AIM:
ALGORITHM:
CODING:
FOR LOOP
x <- letters[4:10]
for(i in x){
print(i)
}
OUTPUT:
[1] "d"
[1] "e"
[1] "f"
[1] "g"
[1] "h"
[1] "i"
[1] "j"
NESTED LOOPS
# Defining matrix
m <- matrix(2:15, 2)
for (r in seq(nrow(m)))
{
for (c in seq(ncol(m))) {
print(m[r, c])
}
}
OUTPUT:
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
[1] 12
[1] 14
[1] 3
[1] 5
[1] 7
[1] 9
[1] 11
[1] 13
WHILE LOOP
x=1
# Print 1 to 5
while(x <= 5){
print(x)
x=x+1
}
OUTPUT:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
x=1
# Print 1 to 5
repeat{
print(x)
x=x+1
if(x > 5){
break
}
}
OUTPUT:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
PROGRAM12 DATE:
ALGORITHM:
CODING:
Apply() function
m1 <- matrix(C<-(1:10),nrow=5, ncol=6)
m1
a_m1 <- apply(m1, 2, sum)
a_m1
OUTPUT:
lapply() function
lapply(X, FUN)
Arguments:
-X: A vector or an object
-FUN: Function applied to each element of x
movies<- c("SPYDERMAN","BATMAN","VERTIGO","CHINATOWN")
movies_lower<-lapply(movies, tolower)
str(movies_lower)
OUTPUT:
## List of 4
## $:chr"spyderman"
## $:chr"batman"
## $:chr"vertigo"
## $:chr"chinatown"
OUTPUT:
sapply(X, FUN)
Arguments:
-X: A vector or an object
-FUN: Function applied to each element of x
We can measure the minimum speed and stopping distances of cars from the cars dataset.
dt<- cars
lmn_cars<- lapply(dt, min)
smn_cars<- sapply(dt, min)
lmn_cars
OUTPUT:
## $speed
## [1] 4
## $dist
## [1] 2
smn_cars
OUTPUT:
## speed dist
## 4 2
lmxcars<- lapply(dt, max)
smxcars<- sapply(dt, max)
lmxcars
OUTPUT:
## $speed
## [1] 25
## $dist
## [1] 120
smxcars
OUTPUT:
## speed dist
## 25 120
We can use a user built-in function into lapply() or sapply(). We create a function named avg to
compute the average of the minimum and maximum of the vector.
avg<- function(x) {
( min(x) + max(x) ) / 2}
fcars<- sapply(dt, avg)
fcars
OUTPUT
## speed dist
## 14.5 61.0