0% found this document useful (0 votes)
12 views3 pages

R Programming: Operators and Control Structures

The document provides an R program that demonstrates the use of various operators, control structures, and functions with default argument values. It includes examples of arithmetic, logical, and miscellaneous operations on vectors and matrices, as well as control flow with if-else statements and loops. Additionally, it defines a function to check divisibility and checks if an object is of complex type.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

R Programming: Operators and Control Structures

The document provides an R program that demonstrates the use of various operators, control structures, and functions with default argument values. It includes examples of arithmetic, logical, and miscellaneous operations on vectors and matrices, as well as control flow with if-else statements and loops. Additionally, it defines a function to check divisibility and checks if an object is of complex type.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab program 3: Write a R program that includes different operators,

control structures, default values for arguments, returning complex objects.


# R program to illustrate
# the use of Arithmetic operators
vec1 <- c(0, 2)
vec2 <- c(2, 3)

# Performing operations on Operands


cat ("Addition of vectors :", vec1 + vec2, "\n")
cat ("Subtraction of vectors :", vec1 - vec2, "\n")
cat ("Multiplication of vectors :", vec1 * vec2, "\n")
cat ("Division of vectors :", vec1 / vec2, "\n")
cat ("Modulo of vectors :", vec1 %% vec2, "\n")
cat ("Power operator :", vec1 ^ vec2)

# R program to illustrate
# the use of Logical operators
vec1 <- c(0,2)
vec2 <- c(TRUE,FALSE)

# Performing operations on Operands


cat ("Element wise AND :", vec1 & vec2, "\n")
cat ("Element wise OR :", vec1 | vec2, "\n")
cat ("Logical AND :", vec1 && vec2, "\n")
cat ("Logical OR :", vec1 || vec2, "\n")
cat ("Negation :", !vec1)
# R program to illustrate
# the use of Miscellaneous operators
mat <- matrix (1:4, nrow = 1, ncol = 4)
print("Matrix elements using : ")
print(mat)

product = mat %*% t(mat)


print("Product of matrices")
print(product,)
cat ("does 1 exist in prod matrix :", "1" %in% product)
# R code to demonstrate Control Structures
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"))
}
# R code to demonstrate loops
x <- letters[4:10]

for(i in x){
print(i)
}
# Function definition to check
# a is divisible by b or not.
# If b is not provided in function call,
# Then divisibility of a is checked with 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)

# R program to check if
# object is of complex type

# Creating a vector
x1 <- (rep(1:4) + 1i*(4:1))
x1

# Calling [Link]() function


[Link](x1)

You might also like