NEP -R PROGRAMMING

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10

PART B

PROGRAM B1 PROGRAM B2 PROGRAM B3 PROGRAM B4 PROGRAM B5 PROGRAM B6 PROGRAM B7 PROGRAM B8 . . .

3

 
  
 
 #Write an R program that includes different operators, control structures, default values for arguments, returning complex objects.


# if and else
x <- 5

# Check value is less than or greater than 10 
if(x > 10){ 
print(paste(x, "is greater than 10")) 
}else{ 
print(paste(x, "is less than 10")) 
}



# Defining matrix 
m <- matrix(2:15, 2) 

for (r in seq(nrow(m))) { 
for (c in seq(ncol(m))) { 
	print(m[r, c]) 
} 
}


# for loop along with concatenate
for (i in c(-8, 9, 11, 45))
{
	print(i)
}




# Default Arguments and complex object return

 
#   3 as default
divisible <- function(a, b = 3){
if(a %% b == 0)
{
	return(paste(a, "is divisible by", b))
}
else
{
	return(paste(a, "is not divisible by", b))
}
}

# Function call
divisible(10, 5)
divisible(12)