1.To count the number of even no in the given vector
x=c(2,5,3,9,8,11,6,44,43,47,67,95,33,65)
count=0
for(val in x)
{
if(val %% 2==0)
count=count+1
}
cat("the given vector has",count,"even number")
output:
the given vector has 4 even number
2.To merge given two list into one
even_list = list (2,4,6,8,10)
odd_list = list (1,3,5,7,9)
new = list (even_list,odd_list)
print(new)
output:
[[1]]
[[1]][[1]]
[1] 2
[[1]][[2]]
[1] 4
[[1]][[3]]
[1] 6
[[1]][[4]]
[1] 8
[[1]][[5]]
[1] 10
[[2]]
[[2]][[1]]
[1] 1
[[2]][[2]]
[1] 3
[[2]][[3]]
[1] 5
[[2]][[4]]
[1] 7
[[2]][[5]]
[1] 9
3.To convert given data frame to list of rows
data = data.frame (x1=1:5,x2=letters[1:5],x3="x")
rownames(data)=paste ("row",1:5)
data_list=split (data,seq(nrow(data)))
print (data_list)
output:
$`1`
x1 x2 x3
row 1 1 a x
$`2`
x1 x2 x3
row 2 2 b x
$`3`
x1 x2 x3
row 3 3 c x
$`4`
x1 x2 x3
row 4 4 d x
$`5`
x1 x2 x3
row 5 5 e x
4.To create a list containing string,numbers,and logical valuesa=list("python","PHP",c(5,7,9,11),TRUE,125.17,75.8)
print("data of the list :")
print(a)
output:
1] "data of the list :"
> print(a)
[[1]]
[1] "python"
[[2]]
[1] "PHP"
[[3]]
[1] 5 7 9 11
[[4]]
[1] TRUE
[[5]]
[1] 125.17
[[6]]
[1] 75.8
5.To extract the five of the levels of factor created from a random sample from the lettersL=sample (LETTERS ,size=10,replace =TRUE)
print("orginal data")
print(L)
f=factor(L)
print("orginal factor:")
print(f)
print("five of the levels")
print(table(L[1:5]))
output:
[1] "orginal data"
> print(L)
[1] "P" "M" "Z" "C" "B" "A" "B" "U" "M" "D"
> f=factor(L)
> print("orginal factor:")
[1] "orginal factor:"
> print(f)
[1] P M Z C B A B U M D
Levels: A B C D M P U Z
> print("five of the levels")
[1] "five of the levels"
> print(table(L[1:5]))
B C M P Z
1 1 1 1 1
6.To multiply the 3X3 matrixdata<-c(1,2,3,0,1,2,0,0,1)
a<-matrix(data,nrow=3,ncol=3)
data<-c(0,1,1,1,0,3,1,3,3)
b<-matrix(data,nrow=3,ncol=3)
ab<-a%*%b
print("matrix a")
print(a)
print("matrix b")
print(b)
print("matrix multipilication result")
print(ab)
output:
[1] "matrix a"
> print(a)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 2 1 0
[3,] 3 2 1
> print("matrix b")
[1] "matrix b"
> print(b)
[,1] [,2] [,3]
[1,] 0 1 1
[2,] 1 0 3
[3,] 1 3 3
> print("matrix multipilication result")
[1] "matrix multipilication result"
> print(ab)
[,1] [,2] [,3]
[1,] 0 1 1
[2,] 1 2 5
[3,] 3 6 12
>
7.To convert the given matrix to a list x<-matrix(1:12,ncol=3)
print("original matrix:")
print(x)
print("list from matrix:")
l=split(x,rep(1:ncol(x),each=nrow(x)))
print(l)
output:
[1] "original matrix:"
> print(x)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> print("list from matrix:")
[1] "list from matrix:"
> l=split(x,rep(1:ncol(x),each=nrow(x)))
> print(l)
$`1`
[1] 1 2 3 4
$`2`
[1] 5 6 7 8
$`3`
[1] 9 10 11 12
>
8.To add 10 to each element of the 1st vector in a given lista<-list(g1=1:10,g2="APCAW",g3="BCA DEPT")
print("Original list:")
print(a)
print("new list")
a$g1=a$g1+10
print(a$g1)
output:
[1] "Original list:"
> print(a)
$g1
[1] 1 2 3 4 5 6 7 8 9 10
$g2
[1] "APCAW"
$g3
[1] "BCA DEPT"
> print("new list")
[1] "new list"
> a$g1=a$g1+10
> print(a$g1)
[1] 11 12 13 14 15 16 17 18 19 20
9.To get the details of the object in the memory name="python"
num1=8
num2=1.5
nums=c(10,20,30,40,50,60)
print(ls())
print("detail of the object in the memory")
print(ls.str())
output:
name : chr "python"
num1 : num 8
num2 : num 1.5
nums : num [1:6] 10 20 30 40 50 60
11.To check whether the given year is a leap year or not
y=as.numeric(readline(prompt='Enter year:'))
if((y%%4==0) && (y%%100!=0)||(y%%400==0))
{
print(paste(y,'Is a leap year'))
}else
{
print(paste(y,'Is not a leap year'))
}
output:
Enter year:2024
"2024 Is a leap year"
Enter year:2005
"2005 Is not a leap year"
12.To check student grade based on marks using control structures
{
mark1=as.integer(readline(prompt="Enter mark of the first subject:"))
mark2=as.integer(readline(prompt="Enter mark of the second subject:"))
mark3=as.integer(readline(prompt="Enter mark of the third subject:"))
mark4=as.integer(readline(prompt="Enter mark of the fourth subject:"))
mark5=as.integer(readline(prompt="Enter mark of the fifth subject:"))
}
tot=mark1+mark2+mark3+mark4+mark5
avg=tot/5
print(tot)
print(avg)
if(avg>=90)
{
print("Grade:A")
}else if(avg >=80 && avg<90)
{
print("Grade:B")
}else if(avg >=70 && avg<80)
{
print("Grade:C")
}else if(avg >=60 && avg<70)
{
print("Grade:D")
}else
{
print("Grade:F")
}
output:
Enter mark of the first subject:55
Enter mark of the second subject:98
Enter mark of the third subject:74
Enter mark of the fourth subject:69
Enter mark of the fifth subject:88
print(tot)
[1] 384
print(avg)
[1] 76.8
[1] "Grade:C"
13.To check the given number is a polindrone or not
n=as.integer(readline(prompt="enter an integer number:"))
a=0
m=n
while(m>0){
r=m%%10
m=m%/%10
a=(a*10)+r
}
if(n==a)
{
print("the given number is a palindrome num")
}else
{
print("the given num is not a palindrome num")
}
output:
enter an integer number:575
"the given number is a palindrome num"
enter an integer number:572
"the given number is not a palindrome num"
14.To find the greatest number among the three given integer
{
x=as.integer(readline(prompt="enter the first number:"))
y=as.integer(readline(prompt="enter the second number:"))
z=as.integer(readline(prompt="enter the third number:"))
}
if (x>y&&x>z)
{
cat("greatest is",x)
}else if(y>x&&y>z)
{
cat("greatest is",y)
}else
{
cat("greatest is",z)
}
output:
enter the first number:85
enter the second number:95
enter the third number:26
greatest is 95
15.To prepare the bill for stationery store
rice=50
sugar=40
dal=90
oil=100
sum=0
repeat {
print("1.rice")
print("2.sugar")
print("3.dal")
print("4.oil")
print("0.generate bill")
o=as.integer(readline(prompt="select an option..."))
if(o==0){
break
}else if(o==1){
q=as.integer(readline(prompt="quantity of rice="))
sum=sum+(q*rice)
}else if(o==2)
{
q=as.integer(readline(prompt="quantity of sugar="))
sum=sum+(q*sugar)
}else if(o==3)
{
q=as.integer(readline(prompt="quantity of dal="))
sum=sum+(q*dal)
}else if(o==4)
{
q=as.integer(readline(prompt="quantity of oil="))
sum=sum+(q*oil)
}
cat("the total cast is RS.",sum)
}
output:
[1] "1.rice"
[1] "2.sugar"
[1] "3.dal"
[1] "4.oil"
[1] "0.generate bill"
select an option...3
quantity of dal=2
the total cast is RS. 180[1]
16.To find the sum of series using recursive function
sum.series<-function (number)
{
if(number == 0){
return (0)
}else
{
return((number * number)+sum.series(number -1))
}
}
sum.series(5)
output:
[1] 55
Comments
Post a Comment