if

x<-20

if(x>10)
  print("greater than 10")
## [1] "greater than 10"

if else

x<- 2
if(x> 0){
  print("positive")
}else{
  print("Negative")

}
## [1] "positive"

else if

x<- 2000
if(x<1000){
  print("movie")
} else if(x>1500){
  print("long drive with frnds")
} else {
    print("sleep")
  }
## [1] "long drive with frnds"

nested if

x<-5000
if(x<4000){
  if(x<2000){
    print("party")
  }else{
    print("holy")
  }
}else{
  print("pondichary")
}
## [1] "pondichary"

pratice

money<-80000
if(money<30000){
  print("buy bike")
}else if(money<50000){
  print("buy car")
}else if(money<70000){
  print("buy audi")
}else if(money<90000){
  print("buy aeroplain")
}else{
  print("buy cycle")
}
## [1] "buy aeroplain"

for loop

for(i in 1:4){
  for(j in 1:10){
    print(paste("step",i,j))
  }
}
## [1] "step 1 1"
## [1] "step 1 2"
## [1] "step 1 3"
## [1] "step 1 4"
## [1] "step 1 5"
## [1] "step 1 6"
## [1] "step 1 7"
## [1] "step 1 8"
## [1] "step 1 9"
## [1] "step 1 10"
## [1] "step 2 1"
## [1] "step 2 2"
## [1] "step 2 3"
## [1] "step 2 4"
## [1] "step 2 5"
## [1] "step 2 6"
## [1] "step 2 7"
## [1] "step 2 8"
## [1] "step 2 9"
## [1] "step 2 10"
## [1] "step 3 1"
## [1] "step 3 2"
## [1] "step 3 3"
## [1] "step 3 4"
## [1] "step 3 5"
## [1] "step 3 6"
## [1] "step 3 7"
## [1] "step 3 8"
## [1] "step 3 9"
## [1] "step 3 10"
## [1] "step 4 1"
## [1] "step 4 2"
## [1] "step 4 3"
## [1] "step 4 4"
## [1] "step 4 5"
## [1] "step 4 6"
## [1] "step 4 7"
## [1] "step 4 8"
## [1] "step 4 9"
## [1] "step 4 10"
for(i in 1:4)
  {
  for(j in 1:10){
    if(j<6)
    print(paste("step",i,5))
    else
    print(paste("step",i,10))
   
  }
}
## [1] "step 1 5"
## [1] "step 1 5"
## [1] "step 1 5"
## [1] "step 1 5"
## [1] "step 1 5"
## [1] "step 1 10"
## [1] "step 1 10"
## [1] "step 1 10"
## [1] "step 1 10"
## [1] "step 1 10"
## [1] "step 2 5"
## [1] "step 2 5"
## [1] "step 2 5"
## [1] "step 2 5"
## [1] "step 2 5"
## [1] "step 2 10"
## [1] "step 2 10"
## [1] "step 2 10"
## [1] "step 2 10"
## [1] "step 2 10"
## [1] "step 3 5"
## [1] "step 3 5"
## [1] "step 3 5"
## [1] "step 3 5"
## [1] "step 3 5"
## [1] "step 3 10"
## [1] "step 3 10"
## [1] "step 3 10"
## [1] "step 3 10"
## [1] "step 3 10"
## [1] "step 4 5"
## [1] "step 4 5"
## [1] "step 4 5"
## [1] "step 4 5"
## [1] "step 4 5"
## [1] "step 4 10"
## [1] "step 4 10"
## [1] "step 4 10"
## [1] "step 4 10"
## [1] "step 4 10"

function

sujith<-function(a,b){

add=a+b
print(paste("addition of ",a, "+",b," is ", add))

}


sujith(4,3)   ## calling with args
## [1] "addition of  4 + 3  is  7"
sujith(2,3)
## [1] "addition of  2 + 3  is  5"
ravi<-function(a,b){


for(i in 1:a){
  for(j in 1:b){
    print(paste("step",i,j))
  }
}



}

ravi(4,10)
## [1] "step 1 1"
## [1] "step 1 2"
## [1] "step 1 3"
## [1] "step 1 4"
## [1] "step 1 5"
## [1] "step 1 6"
## [1] "step 1 7"
## [1] "step 1 8"
## [1] "step 1 9"
## [1] "step 1 10"
## [1] "step 2 1"
## [1] "step 2 2"
## [1] "step 2 3"
## [1] "step 2 4"
## [1] "step 2 5"
## [1] "step 2 6"
## [1] "step 2 7"
## [1] "step 2 8"
## [1] "step 2 9"
## [1] "step 2 10"
## [1] "step 3 1"
## [1] "step 3 2"
## [1] "step 3 3"
## [1] "step 3 4"
## [1] "step 3 5"
## [1] "step 3 6"
## [1] "step 3 7"
## [1] "step 3 8"
## [1] "step 3 9"
## [1] "step 3 10"
## [1] "step 4 1"
## [1] "step 4 2"
## [1] "step 4 3"
## [1] "step 4 4"
## [1] "step 4 5"
## [1] "step 4 6"
## [1] "step 4 7"
## [1] "step 4 8"
## [1] "step 4 9"
## [1] "step 4 10"
s<-function(a){
  if(a<0){
    print("Negative")
  }else if(a>0){
    print("postive")
  }else
    print("0")
  
  
}
  
  s(1)
## [1] "postive"
d<-function(v){

r=rnorm(v)
b=mean(r)
print(b)
}


d(5)
## [1] 0.4246031
b<-function(a){
  k=0
  c=0
  z=0
  for(p  in 0:5){
    for(q in 0:5){
      c=c+1
      k[c]<-2^p*3^q
       
      }
  }
  k=sort(k)
   z=k[a]%%2
  if(z==0){
    print(paste(k[a],"=","even"))
  }else
    print(paste(k[a],"=","odd"))
}

b(5)

Sys.time()
## [1] "2015-10-21 18:08:15 IST"
l<-data.frame(dates=c("2015-10-2 14:54:28 IST","2015-10-1 14:54:28 IST","2015-10-22 14:54:28 IST"))
l
##                     dates
## 1  2015-10-2 14:54:28 IST
## 2  2015-10-1 14:54:28 IST
## 3 2015-10-22 14:54:28 IST
p<-as.POSIXlt(l$dates)

names(unclass(p))
##  [1] "sec"    "min"    "hour"   "mday"   "mon"    "year"   "wday"  
##  [8] "yday"   "isdst"  "zone"   "gmtoff"
l$sec<-p$sec

l
##                     dates sec
## 1  2015-10-2 14:54:28 IST  28
## 2  2015-10-1 14:54:28 IST  28
## 3 2015-10-22 14:54:28 IST  28
l$dif<-(as.POSIXlt(Sys.time())$sec-l$sec)

l
##                     dates sec       dif
## 1  2015-10-2 14:54:28 IST  28 -12.23732
## 2  2015-10-1 14:54:28 IST  28 -12.23732
## 3 2015-10-22 14:54:28 IST  28 -12.23732