f<-function(x)
{
flag=0
if(x==1)
{
print(" 1 is nor prime nor composite")
}else if(x==2)
{
print("2 is a prime number")
}else
{
m=x/2
for(i in 2:m)
{
if(x%%i==0)
{
print(paste(x,"is not a prime number"))
flag=1
break
}
}
if(flag==0)
{
print(paste(x,"is prime number"))
}
}
}
f(1)
## [1] " 1 is nor prime nor composite"
f(2)
## [1] "2 is a prime number"
f(3)
## [1] "3 is prime number"
f(4)
## [1] "4 is not a prime number"
for(i in 1:10)
{
print(paste("Multiplication table of ", i))
for(j in 1:10)
{
print(paste(i,"*",j,"=",i*j))
}
}
## [1] "Multiplication table of 1"
## [1] "1 * 1 = 1"
## [1] "1 * 2 = 2"
## [1] "1 * 3 = 3"
## [1] "1 * 4 = 4"
## [1] "1 * 5 = 5"
## [1] "1 * 6 = 6"
## [1] "1 * 7 = 7"
## [1] "1 * 8 = 8"
## [1] "1 * 9 = 9"
## [1] "1 * 10 = 10"
## [1] "Multiplication table of 2"
## [1] "2 * 1 = 2"
## [1] "2 * 2 = 4"
## [1] "2 * 3 = 6"
## [1] "2 * 4 = 8"
## [1] "2 * 5 = 10"
## [1] "2 * 6 = 12"
## [1] "2 * 7 = 14"
## [1] "2 * 8 = 16"
## [1] "2 * 9 = 18"
## [1] "2 * 10 = 20"
## [1] "Multiplication table of 3"
## [1] "3 * 1 = 3"
## [1] "3 * 2 = 6"
## [1] "3 * 3 = 9"
## [1] "3 * 4 = 12"
## [1] "3 * 5 = 15"
## [1] "3 * 6 = 18"
## [1] "3 * 7 = 21"
## [1] "3 * 8 = 24"
## [1] "3 * 9 = 27"
## [1] "3 * 10 = 30"
## [1] "Multiplication table of 4"
## [1] "4 * 1 = 4"
## [1] "4 * 2 = 8"
## [1] "4 * 3 = 12"
## [1] "4 * 4 = 16"
## [1] "4 * 5 = 20"
## [1] "4 * 6 = 24"
## [1] "4 * 7 = 28"
## [1] "4 * 8 = 32"
## [1] "4 * 9 = 36"
## [1] "4 * 10 = 40"
## [1] "Multiplication table of 5"
## [1] "5 * 1 = 5"
## [1] "5 * 2 = 10"
## [1] "5 * 3 = 15"
## [1] "5 * 4 = 20"
## [1] "5 * 5 = 25"
## [1] "5 * 6 = 30"
## [1] "5 * 7 = 35"
## [1] "5 * 8 = 40"
## [1] "5 * 9 = 45"
## [1] "5 * 10 = 50"
## [1] "Multiplication table of 6"
## [1] "6 * 1 = 6"
## [1] "6 * 2 = 12"
## [1] "6 * 3 = 18"
## [1] "6 * 4 = 24"
## [1] "6 * 5 = 30"
## [1] "6 * 6 = 36"
## [1] "6 * 7 = 42"
## [1] "6 * 8 = 48"
## [1] "6 * 9 = 54"
## [1] "6 * 10 = 60"
## [1] "Multiplication table of 7"
## [1] "7 * 1 = 7"
## [1] "7 * 2 = 14"
## [1] "7 * 3 = 21"
## [1] "7 * 4 = 28"
## [1] "7 * 5 = 35"
## [1] "7 * 6 = 42"
## [1] "7 * 7 = 49"
## [1] "7 * 8 = 56"
## [1] "7 * 9 = 63"
## [1] "7 * 10 = 70"
## [1] "Multiplication table of 8"
## [1] "8 * 1 = 8"
## [1] "8 * 2 = 16"
## [1] "8 * 3 = 24"
## [1] "8 * 4 = 32"
## [1] "8 * 5 = 40"
## [1] "8 * 6 = 48"
## [1] "8 * 7 = 56"
## [1] "8 * 8 = 64"
## [1] "8 * 9 = 72"
## [1] "8 * 10 = 80"
## [1] "Multiplication table of 9"
## [1] "9 * 1 = 9"
## [1] "9 * 2 = 18"
## [1] "9 * 3 = 27"
## [1] "9 * 4 = 36"
## [1] "9 * 5 = 45"
## [1] "9 * 6 = 54"
## [1] "9 * 7 = 63"
## [1] "9 * 8 = 72"
## [1] "9 * 9 = 81"
## [1] "9 * 10 = 90"
## [1] "Multiplication table of 10"
## [1] "10 * 1 = 10"
## [1] "10 * 2 = 20"
## [1] "10 * 3 = 30"
## [1] "10 * 4 = 40"
## [1] "10 * 5 = 50"
## [1] "10 * 6 = 60"
## [1] "10 * 7 = 70"
## [1] "10 * 8 = 80"
## [1] "10 * 9 = 90"
## [1] "10 * 10 = 100"
g<-function(x)
{
if(x>0)
{
print(sqrt(x))
}else
{
print("NA")
}
}
let’s pass a possitive value
g(5)
## [1] 2.236068
Now, let’s pass a negative value
g(-5)
## [1] "NA"
ho<-function(n)
{
for(i in n)
{
if(i<5 | i>90)
{
print(i*10)
}else
{
print(i*0.1)
}
}
}
ho(1:100)
## [1] 10
## [1] 20
## [1] 30
## [1] 40
## [1] 0.5
## [1] 0.6
## [1] 0.7
## [1] 0.8
## [1] 0.9
## [1] 1
## [1] 1.1
## [1] 1.2
## [1] 1.3
## [1] 1.4
## [1] 1.5
## [1] 1.6
## [1] 1.7
## [1] 1.8
## [1] 1.9
## [1] 2
## [1] 2.1
## [1] 2.2
## [1] 2.3
## [1] 2.4
## [1] 2.5
## [1] 2.6
## [1] 2.7
## [1] 2.8
## [1] 2.9
## [1] 3
## [1] 3.1
## [1] 3.2
## [1] 3.3
## [1] 3.4
## [1] 3.5
## [1] 3.6
## [1] 3.7
## [1] 3.8
## [1] 3.9
## [1] 4
## [1] 4.1
## [1] 4.2
## [1] 4.3
## [1] 4.4
## [1] 4.5
## [1] 4.6
## [1] 4.7
## [1] 4.8
## [1] 4.9
## [1] 5
## [1] 5.1
## [1] 5.2
## [1] 5.3
## [1] 5.4
## [1] 5.5
## [1] 5.6
## [1] 5.7
## [1] 5.8
## [1] 5.9
## [1] 6
## [1] 6.1
## [1] 6.2
## [1] 6.3
## [1] 6.4
## [1] 6.5
## [1] 6.6
## [1] 6.7
## [1] 6.8
## [1] 6.9
## [1] 7
## [1] 7.1
## [1] 7.2
## [1] 7.3
## [1] 7.4
## [1] 7.5
## [1] 7.6
## [1] 7.7
## [1] 7.8
## [1] 7.9
## [1] 8
## [1] 8.1
## [1] 8.2
## [1] 8.3
## [1] 8.4
## [1] 8.5
## [1] 8.6
## [1] 8.7
## [1] 8.8
## [1] 8.9
## [1] 9
## [1] 910
## [1] 920
## [1] 930
## [1] 940
## [1] 950
## [1] 960
## [1] 970
## [1] 980
## [1] 990
## [1] 1000
Let’s first look how the iris dataset look like.
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
let’s look at the structure of iris dataset.
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Solve the problem using ifelse
a<-ifelse(iris$Sepal.Length>5,"greater than 5","lesser than 5")
a
## [1] "greater than 5" "lesser than 5" "lesser than 5" "lesser than 5"
## [5] "lesser than 5" "greater than 5" "lesser than 5" "lesser than 5"
## [9] "lesser than 5" "lesser than 5" "greater than 5" "lesser than 5"
## [13] "lesser than 5" "lesser than 5" "greater than 5" "greater than 5"
## [17] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [21] "greater than 5" "greater than 5" "lesser than 5" "greater than 5"
## [25] "lesser than 5" "lesser than 5" "lesser than 5" "greater than 5"
## [29] "greater than 5" "lesser than 5" "lesser than 5" "greater than 5"
## [33] "greater than 5" "greater than 5" "lesser than 5" "lesser than 5"
## [37] "greater than 5" "lesser than 5" "lesser than 5" "greater than 5"
## [41] "lesser than 5" "lesser than 5" "lesser than 5" "lesser than 5"
## [45] "greater than 5" "lesser than 5" "greater than 5" "lesser than 5"
## [49] "greater than 5" "lesser than 5" "greater than 5" "greater than 5"
## [53] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [57] "greater than 5" "lesser than 5" "greater than 5" "greater than 5"
## [61] "lesser than 5" "greater than 5" "greater than 5" "greater than 5"
## [65] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [69] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [73] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [77] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [81] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [85] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [89] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [93] "greater than 5" "lesser than 5" "greater than 5" "greater than 5"
## [97] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [101] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [105] "greater than 5" "greater than 5" "lesser than 5" "greater than 5"
## [109] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [113] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [117] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [121] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [125] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [129] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [133] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [137] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [141] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [145] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [149] "greater than 5" "greater than 5"
Solve the problem using apply() function. * First create a user-defined function. * Then use apply function
j<-function(sepl_length)
{
ifelse(iris$Sepal.Length>5,"greater than 5","lesser than 5")
}
Now use apply function to use the above function on the dataset.
b<-apply(iris[,c(-3:-5)],2,j) # Note that apply is for matrix not vector so, i am passing 2 columns
b[,"Sepal.Length"]
## [1] "greater than 5" "lesser than 5" "lesser than 5" "lesser than 5"
## [5] "lesser than 5" "greater than 5" "lesser than 5" "lesser than 5"
## [9] "lesser than 5" "lesser than 5" "greater than 5" "lesser than 5"
## [13] "lesser than 5" "lesser than 5" "greater than 5" "greater than 5"
## [17] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [21] "greater than 5" "greater than 5" "lesser than 5" "greater than 5"
## [25] "lesser than 5" "lesser than 5" "lesser than 5" "greater than 5"
## [29] "greater than 5" "lesser than 5" "lesser than 5" "greater than 5"
## [33] "greater than 5" "greater than 5" "lesser than 5" "lesser than 5"
## [37] "greater than 5" "lesser than 5" "lesser than 5" "greater than 5"
## [41] "lesser than 5" "lesser than 5" "lesser than 5" "lesser than 5"
## [45] "greater than 5" "lesser than 5" "greater than 5" "lesser than 5"
## [49] "greater than 5" "lesser than 5" "greater than 5" "greater than 5"
## [53] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [57] "greater than 5" "lesser than 5" "greater than 5" "greater than 5"
## [61] "lesser than 5" "greater than 5" "greater than 5" "greater than 5"
## [65] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [69] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [73] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [77] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [81] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [85] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [89] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [93] "greater than 5" "lesser than 5" "greater than 5" "greater than 5"
## [97] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [101] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [105] "greater than 5" "greater than 5" "lesser than 5" "greater than 5"
## [109] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [113] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [117] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [121] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [125] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [129] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [133] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [137] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [141] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [145] "greater than 5" "greater than 5" "greater than 5" "greater than 5"
## [149] "greater than 5" "greater than 5"
fib_series<-function(x)
{
n1=0
n2=1
if(x<0)
{
print("The number should not be less than zero")
}else if(x==1)
{
print(x)
}else
{
print(n1)
print(n2)
for(i in 3:x)
{
n3=n1+n2
print(n3)
n1=n2
n2=n3
}
}
}
Now, we can specify a range.
fib_series(8)
## [1] 0
## [1] 1
## [1] 1
## [1] 2
## [1] 3
## [1] 5
## [1] 8
## [1] 13
fun<-function(x)
{
sum_ser=0
for(i in 1:x)
{
sum_ser<-sum_ser+(i^2)
}
print(sum_ser)
}
fun(5)
## [1] 55
fun_rec<-function(y)
{
if(y==0)
{
return(0)
}else{
return((y*y)+fun_rec(y-1))
}
}
fun_rec(5)
## [1] 55
v<-1:100 # Let k value be 100
v
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
## [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
## [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
## [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
## [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
j<-ifelse(v%%3==0,TRUE,FALSE) # If the number is divisible, then true else false
v[j] # Displays only those numbers whose index is true
## [1] 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69
## [24] 72 75 78 81 84 87 90 93 96 99
length(v[j]) # verify its length
## [1] 33
fun_rec<-function(y)
{
if(y==0)
{
return(0)
}else{
return(y+fun_rec(y-1))
}
}
fun_rec(5)
## [1] 15