if
x<-3
if(x<5){
print(x)}
## [1] 3
if else
y<-6
if(y>5){
if(is.integer(y)){
print(y)
} else{
print("not an integer")
}
}
## [1] "not an integer"
for loop
x<-c("a","b","c")
for(i in seq_along(x))
{
print(x[i])
}
## [1] "a"
## [1] "b"
## [1] "c"
nested if
money<-1500
if(money!=0){
print("party")
if(money>1000){
print("yes money >1000")
}else{
print("no,money <1000")
}
}else{
print("nothing")
}
## [1] "party"
## [1] "yes money >1000"
nested for loop
for(i in 1:2){
for(j in 1:10){
if(j<=5){
print(paste(i,"step",5))
}else{
print(paste(i,"step",10))
}
}
}
## [1] "1 step 5"
## [1] "1 step 5"
## [1] "1 step 5"
## [1] "1 step 5"
## [1] "1 step 5"
## [1] "1 step 10"
## [1] "1 step 10"
## [1] "1 step 10"
## [1] "1 step 10"
## [1] "1 step 10"
## [1] "2 step 5"
## [1] "2 step 5"
## [1] "2 step 5"
## [1] "2 step 5"
## [1] "2 step 5"
## [1] "2 step 10"
## [1] "2 step 10"
## [1] "2 step 10"
## [1] "2 step 10"
## [1] "2 step 10"
using for loop and if condition in data frame
d<-data.frame(a=c(1:10),b=c(20,30))
d
## a b
## 1 1 20
## 2 2 30
## 3 3 20
## 4 4 30
## 5 5 20
## 6 6 30
## 7 7 20
## 8 8 30
## 9 9 20
## 10 10 30
for(i in 1:nrow(d)){
if(d$a[i]<=5){
d$a[i]<-0
}else{
d$a[i]<-1
}
if(d$b[i]==20){
d$b[i]<-0
}else{
d$b[i]<-1
}
}
d
## a b
## 1 0 0
## 2 0 1
## 3 0 0
## 4 0 1
## 5 0 0
## 6 1 1
## 7 1 0
## 8 1 1
## 9 1 0
## 10 1 1
d<-data.frame(name=c("manasa","shyama","navya","leela","rekha"),age=c(25,43,24,55,23))
d
## name age
## 1 manasa 25
## 2 shyama 43
## 3 navya 24
## 4 leela 55
## 5 rekha 23
if(d$age>25){
}
## Warning in if (d$age > 25) {: the condition has length > 1 and only the
## first element will be used
function arguments
m<-function(buildup=0,sleep=0,extracurricular=0,smile=0,style=0,height=0,age=0,speech=0){
marks<-buildup+sleep+extracurricular+style+speech+smile+height+age
return(marks)
}
m()
## [1] 0
a<-function(money)
{
if(money!=0){
print("party")
if(money>1000){
print("yes money >1000")
}else{
print("no,money <1000")
}
}else{
print("nothing")
}
}
a(1000)
## [1] "party"
## [1] "no,money <1000"
leela<-function(a,b)
{
c<-a*b
return(c)
}
leela(3,2)
## [1] 6
t<-function(a,b)
{
for(i in 1:a)
{
for(j in 1:b)
{
print(paste(i,"step",j))
}
}
}
t(4,5)
## [1] "1 step 1"
## [1] "1 step 2"
## [1] "1 step 3"
## [1] "1 step 4"
## [1] "1 step 5"
## [1] "2 step 1"
## [1] "2 step 2"
## [1] "2 step 3"
## [1] "2 step 4"
## [1] "2 step 5"
## [1] "3 step 1"
## [1] "3 step 2"
## [1] "3 step 3"
## [1] "3 step 4"
## [1] "3 step 5"
## [1] "4 step 1"
## [1] "4 step 2"
## [1] "4 step 3"
## [1] "4 step 4"
## [1] "4 step 5"
k<-function(a){
if(a>0)
{
print("The number is positive")
}else{
print("The number is negative")
}
}
k(12)
## [1] "The number is positive"
k(-12)
## [1] "The number is negative"
mean of function
u<-function(a)
{
z<-rnorm(a)
print(z)
mean(z)
}
u(5)
## [1] -2.6824248 -0.3127922 0.3625019 -0.1574057 0.5181197
## [1] -0.4544002
finding mean of elements given dynamically
p<-function(a,b,c)
{
s<-c(a,b,c)
w<-mean(s)
print(w)
}
p(1,2,3)
## [1] 2
for loop
r<-function(n)
{ a=0
k=0
for(p in 0:5)
{
for(q in 0:5)
{
a=a+1
k[a]<-(2^p*3^q)
}
}
k<-sort(k)
if((k[n]%%2)==0)
{
sprintf("%d number is even",k[n])
}else{
print("%d number is odd",k[n])
}
}
r(5)
## [1] "6 number is even"