0% found this document useful (0 votes)
75 views

Chandan R Practical File 3 Sem

R language
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Chandan R Practical File 3 Sem

R language
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

WORLD COLLEGE OF TECHNOLOGY AND MANAGEMENT

COMPUTER SCIENCE AND DESIGN

PRACTICAL FILE

SESSION:-2022

SUBMITTED BY:- SUBMITTED TO:-


Mrs. TANVI RUSTAGI
NAME: - CHANDAN GIRI
COURSE: - B.TECH 3rd sem. ASSISTANT. PROFESSOR
R
BRANCH: - CSD
(WCTM)
SUBJECT: - R Programming
ROLL NUMBER:- 9038985

CHANDAN GIRI (CSD) 3rd Sem Page 1


INDEX

S.NO TITLE

1. Implementation ofAlgebra using with or without variable declaration


2. WAP to solve Algebraic expression with or without variable declaration
3. WAP to implement Relational operators( = , < , > , == , != )
4. WAP to implement Logical Operators ( & , | , ! , && , || )&
introduction of Vectors.
5. Introduction to Assignment(= ,<-, ->, <<-, ->>)and Miscellaneous
operators(
:,%in%,%*%)
6. WAP to implement Class()and Type of() functions.
7. WAP to use Google collab for taking input from users.
8. WAP to implement ATOMIC VECTOR

9. WAP to perform Operations on Vector

10. WAP to perform List implementation

11. WAP to perform Array implementation

12. WAP to implement IF ELSE family

13. WAP to implement if else function and switch case statement

14. WAP to implement loops in R

15. WAP to implement built-in and user defined function

CHANDAN GIRI (CSD) 3rd Sem Page 2


Program1

Implementation f Algebra using with or without


variabledeclaration
>#Withoutvariable

Code:-
> 2+8 #Input
[1]10 #Output

#With variable
> A=50 #HereAisaVariablename
>A #ToPrintA
[1]50 #Output
> B=30 #HereBisaVariablename
>B #ToPrintB
[1]20 #Output
> C=A+B #Toaddtwonumbers
>C #ToPrintC
> [1] 80 #Output

CHANDAN GIRI (CSD) 3rd Sem Page 3


Coding:-
a=25
b=50
print(a+b)# addition
print(a-b)#subtraction
print(a*b)#multiply
print(a/b)#divide
print(a%%b)#modulas

Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 4


Program2

WAP to solve algebraic expression with or without variable


declaration

Coding:-
1+2/3-2*6.5
Output:-
Expression is solved as per BODMAS rule in order from left to right

Coding:-
1*(2/(1+1))
Output:-
Theorderofoperationscancontrolledbyusingparenthesis.Operationsarepe
rformedfrominnerParenthesisoutward

Algebraic Expression with variable


Coding:-
a=1*2/1+1
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 5


Program3

WAP to implement Relational Operators ( = , < , > , ==, != )


Coding:-
Var1=50 #Input
Var2=30 #Input

Var1<Var2 #Condition

Output:-

> Var1>Var2 #Condition2


[1] TRUE
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 6


Coding:-
a=23
b=40
print(a<b)
print(a>b)
print(a>=b)
print(a<=b)
print(a==b)
print(a!=b)

Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 7


Program4

WAP to implement Logical Operators ( & , | , ! , && , || ) and


Introduction of Vectors
&&=AND
||=OR
&=elementwiseAND
|=elementwiseOR
!=elementwiseNOT
0 is consider as negative value and -1 to +1 is consider as positive value
AND (&&) operator =When both conditions are True OR (||) operator =
When any of the condition is True

Coding:-
a=20
b=30
print(a&&b)
print(a||b)
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 8


&=elementwiseAND
|=elementwiseOR
!=elementwiseNOT
Coding:-
vector1 =c(1,2,3,4,5)

vector2=c(11,21,31,41,51)
print(vector1&vector2)
print(vector1|vector2)
print(!vector2)
Output:-

Coding:-
vector1 =c(1,2,3,-
4,0)vector2=c(11,0,31,41,5
1)
print(vector1&vector2)
print(vector1|vector2)
print(!vector2)
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 9


Program5

Introduction to Assignment(= ,<-, ->, <<-, ->>)and Miscellaneous


operators( :,%in%,%*%)
=,<-,<<-(LEFTAssignment)
->>,->(RIGHTAssignment)

Coding:-

A=12

B<-23
CC<<-55
12->D
55->>E
print(A)
print(B)
print(C)
print(D)
print(E)
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 10


Miscellaneous operators(:,%in%,%*%)
(:)used to discuss series of values
(%in%) used to search given value in series(%*%)used
to multiply series from given value

Coding:-
vector1=1:18
print(vector1)
vector1=1
print(5%in%vector1)
vector1=1:18
print(5%*%vector1)
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 11


Program6

WAP to implement Class()and Type of() functions.


class()- What kind of object is it (high level)?
typeof()- What is the object data type (lowlevel)?
Coding:-
Var1=’WXYZ’
class(Var1)
typeof(Var1)

Var2=12.2
class(Var2)
typeof(Var2)

Var3=TRUE
class(Var3)
typeof(Var3)

Var4=25+5i
Class(Var4)
typeof(Var4)

Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 12


Program7

WAP to use Google collab for taking input from users.

RunningonGoogleCollab
Coding:-
myname<-readline(prompt="enter your name")
mygender<-readline(prompt="enter your gender")
mycity<-readline(prompt="enter your city")
myage=readline(prompt="enter your age")
myage=as.integer((myage))
print(myname)
print(mygender)
print(mycity)
print(myage)

Output:-
enter your vishal Sharma
enter your gender male
enter your city New Delhi
enter your age 19

[1] "vishal Sharma"


[1] "male"
[1] "New Delhi"
[1] 24

CHANDAN GIRI (CSD) 3rd Sem Page 13


Program8
WAP to implement ATOMIC VECTOR(similar data type)
Coding:-
a=1:10
a
b=seq(1,10)
b
c=seq(1,5,by=2)
c
vect1=c(1,2,5,77,88)
vect1
typeof(vect1)
class(vect1)

vect2=c(1,2,5,77,88)
vect2=
as.integer(vect2)typeof(vect2)
class(vect2)

vect3=c("XYZ","ABC")
vect3
typeof(vect3)
class(vect3)
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 14


Program 9
WAP to perform Operations on Vectors

Coding:-
vect1=c(5,7,9)
vect2=c(2,4,6)
print(vect1+vect2)
print(vect1*vect2)
print(vect1/vect2)
print(vect1-vect2)
print(vect1%%vect2)
merge=c(vect1,vect2)
merge
vect5[2:7]
vect5
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 15


Program 10
WAP to perform operations on List datastructure
Coding:-

vec<-c(3,4,5,6)
chvec<-c("RAM","SHYAM","CS","tutorial")
lovec<-c(TRUE,FALSE,FALSE,TRUE)

olist<-list(vec,chvec,lovec) olist

olist[1]
olist[2]
olist[3]

Case II
list1<-list('XYZ',"ABC",2,2.56,76L,TRUE,3+7i)
list1

class(list1)

typeof(list1)

Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 16


Case II output:-

Manipulate
list_record[1]<-"StudentsNames"

list_record

list_record[1] <- NULLlist_record

list_record[1]

CHANDAN GIRI (CSD) 3rd Sem Page 17


Program11

WAP to perform Array implementation


Coding:-
#Arraydeclaration
v1<-c(1,2,3)
v2<-c(11,22,33,44,55,95)

arr<-array(c(v1,v2),dim=c(3,3,2))print(arr)
typeof(arr)
class(arr)

Output:-

#Namingcolumnsandrow

v11<-c(1,2,3)
v22<-c(11,22,33,44,55,95)

col<-c("Col1","Col2","Col3")
CHANDAN GIRI (CSD) 3rd Sem Page 18
row<-c("Row1","Row2","Row3")mat<-c("Matrix1","Matrix2")

arr<-array(c(v11,v22),dim=c(3,3,2),dimnames=list(row,col,mat))

print(arr)
typeof(arr)
class(arr)
Output:-

#Manipulationonanarray

result1<-arr[1:3]
result2<-arr[2:3]
merge<-result1+result2merge
#convertvalueofaspecificindexarr[1
]=10
arr

CHANDAN GIRI (CSD) 3rd Sem Page 19


Output:-

#Calculationacrossarrayelements
vec1<-c(1,2,3)
vec2<-c(1,1,1,1,1,1)

res1<-array(c(vec1,vec2),dim=c(3,3,2))

print(res1)

result<-apply(res1,1,sum)

print(result)

CHANDAN GIRI (CSD) 3rd Sem Page 20


Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 21


Program12

WAP to implement if else family


ifelsestatementisagreattoolfordevelopertryingtoreturnanoutputbasedona
condition
Type:

● if
● ifelse
● ifelseif
● if else if
elsesyntax:if(con
dition){expr1
}else{ex
pr2
}

Coding:-
#if
a=-10
if(a>=10)
{print("aispositive")}else
{print("aisnegative")
}
#ifelseif
b=79if(b>
=199)
{print("noisgreaterthan199")
}elseif(b<79)

CHANDAN GIRI (CSD) 3rd Sem Page 22


{print("noisnotgreaterthan199")
}else
{print("default")}
Output:-

Write a program to You are Adult or not


Coding:-
my.age<- readline(prompt="Enter age: ")
my.age
if(my.age>=2004)
{print("You are Not Adult")}else
{
print("You are adult ")
}
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 23


Write a program to check leap year

Coding:-

if((year %% 4) == 0) {
if((year %% 100) == 0) {
if((year %% 400) == 0) {
print(paste(year,"is a leap year"))
} else {
print(paste(year,"is not a leap year"))
}
} else {
print(paste(year,"is a leap year"))
}
} else {
print(paste(year,"is not a leap year"))
}

Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 24


Program13

WAPtoimplementifelsefunction andswitch case statement


ifelsefunction()
Syntax:ifelse(test,yes,no)

Coding:-
#case1
age<-c(22,31,23,42,34,45,34)
ifelse(age%%2==0,"Yes","No")
#case2
age<-c(226,316,236,426,346,456,346)
ifelse(age+10==236,"Yes","No")
#case3
age<-c(226,316,236,426,346,456,346)
ifelse(age-10==226,"Yes","No")
Output:-

SWITCH CASE STATEMENT:A switch case statement allows a


variable to be tested for equality against a list of value.Each value
is called a case,and variable being switched on is checked for each
case.

CHANDAN GIRI (CSD) 3rd Sem Page 25


Write a program to implement multiplication using switch
statement

Coding:-

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)
)

Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 26


Program14

WAP to implement loops in R


case1:ToprintsumofanArray
CODE:
array1<-c(123,245,456,567,768,400,230,545,689,699)
sum=0;
for (i in array1) {

sum = sum +
iprint(i)
}
print(sum)
Output:-

case 2:Nested loop


Coding:-
a <-seq(1:5)

for (i in a)
{print(i)
CHANDAN GIRI (CSD) 3rd Sem Page 27
for (j in a)
{print(j)
}
}

Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 28


Whileloopimplementation:

Coding:-

a <- 1
while(a<=10){ print(a)
a=a+1

b <- 1
while(b==10)
{
print(b)
b=b+1
}
}
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 29


Program 15

WAPtoimplementbuilt-inanduserdefined function

Function:A set of statements which are organized together to perform a specific


taskisknownasafunction.
● Writtentocarryoutaspecifictask
● Mayormaynothaveanarguments
● Containabody
● Mayormaynotreturnoneormoreoutputvalues

Type:
● Built-in:ThatisalreadystoredinRe.gMathematicalfunction.
Userdefined:Wecancreateasperourneed.functionis the keyword to create a function in R
● Syntax:

func_name=Function(arg1,arg2…)
{

Body

}
● The simple printing method in R is to useprint()
● cat()does the same thing but is valid only for atomic types
● An essential difference betweencat()andprint()is the class of the object
theyreturn.Fornow,youcanassumethatclassmeansthetypeofobject.
● Theactualusefulnessofcat()canbeobtainedwhenwehavetwoormorestrings
that we want to concatenate.

CHANDAN GIRI (CSD) 3rd Sem Page 30


Built-infunctions:
print("HELLO,FRIENDS")
cat("HiWCTM","WELCOMETOMY LAPTOP")

p<-c(11,23,34,45,56,67,78,89)
m=mean(
p)print(m
)
z<-c(11,23,34,45,56,67,78,89)

range(z)
Output:-

User defined function


firstFunction<-function(){
sum<-0
for (i in
seq(1:34))
{sum=sum+i
}
print(sum)
}

CHANDAN GIRI (CSD) 3rd Sem Page 31


Output:-

with argument with return


Coding:-

myFunction<-
function(x,y){return(x+y)
}
myFunction(45,35)
Output:-

CHANDAN GIRI (CSD) 3rd Sem Page 32

You might also like