Chandan R Practical File 3 Sem
Chandan R Practical File 3 Sem
PRACTICAL FILE
SESSION:-2022
S.NO TITLE
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
Output:-
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
Var1<Var2 #Condition
Output:-
Output:-
Coding:-
a=20
b=30
print(a&&b)
print(a||b)
Output:-
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:-
Coding:-
A=12
B<-23
CC<<-55
12->D
55->>E
print(A)
print(B)
print(C)
print(D)
print(E)
Output:-
Coding:-
vector1=1:18
print(vector1)
vector1=1
print(5%in%vector1)
vector1=1:18
print(5%*%vector1)
Output:-
Var2=12.2
class(Var2)
typeof(Var2)
Var3=TRUE
class(Var3)
typeof(Var3)
Var4=25+5i
Class(Var4)
typeof(Var4)
Output:-
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
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:-
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:-
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:-
Manipulate
list_record[1]<-"StudentsNames"
list_record
list_record[1]
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
#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)
● 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)
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:-
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:-
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:-
sum = sum +
iprint(i)
}
print(sum)
Output:-
for (i in a)
{print(i)
CHANDAN GIRI (CSD) 3rd Sem Page 27
for (j in a)
{print(j)
}
}
Output:-
Coding:-
a <- 1
while(a<=10){ print(a)
a=a+1
b <- 1
while(b==10)
{
print(b)
b=b+1
}
}
Output:-
WAPtoimplementbuilt-inanduserdefined function
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.
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:-
myFunction<-
function(x,y){return(x+y)
}
myFunction(45,35)
Output:-