R Lab Assignment
R Lab Assignment
# Logical Operations
print("Logical Operations:")
a <- TRUE
b <- FALSE
print(paste("Logical AND: ", a & b))
print(paste("Logical OR: ", a | b))
print(paste("Logical NOT: ", !a))
# Relational Operations
print("Relational Operations:")
m <- 10
n <- 20
print(paste("Greater than: ", m > n))
print(paste("Less than: ", m < n))
print(paste("Greater than or equal to: ", m >= n))
print(paste("Less than or equal to: ", m <= n))
print(paste("Equal to: ", m == n))
print(paste("Not Equal to: ", m != n))
# Assignment Operations
print("Assignment Operations:")
p <- 30
q <- 40
p <- q
print(paste("Assignment: ", p))
Output:
[1] "Arithmetic Operations:"
> x <- 10
> y <- 20
[1] "Addition: 30"
[1] "Subtraction: -10"
[1] "Multiplication: 200"
[1] "Division: 0.5"
[1] "Exponentiation: 100"
[1] "Modulus: 1"
[1] "Integer Division: 3"
Output:
Output:
if(num > 0) {
print(paste(num, "is positive"))
} else if(num < 0) {
print(paste(num, "is negative"))
} else {
print("The number is zero")
}
Output:
Output:
Output:
[1] 8 12 15 23 29 39 56
[1] "Vector sorted in descending order:"
[1] 56 39 29 23 15 12 8
Experiment No 3
Name: Tohidealam Firoj Nadaf
Branch: B.Tech AIML – B
Roll no: 97
Output:
# Initialize index to 1
index <- 1
Output:
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
Experiment No 4
Name: Tohidealam Firoj Nadaf
Branch: B.Tech AIML – B
Roll no: 97
for(num in vec) {
# If the number is not divisible by 2 (i.e., it's odd), skip to the next iteration
if(num %% 2 != 0) {
next
}
# If the number is even, print it
print(num)
}
Output:
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
Output:
[1] TRUE
[1] FALSE
c) Write a program to call a Function with Default Argument in R.
power <- function(base, exponent = 2) {
return(base ^ exponent)
}
# Call the function with one argument (the exponent will default to 2)
print(power(4)) # This will print 16
Output:
[1] 8
[1] 16
Experiment No 5
Name: Tohidealam Firoj Nadaf
Branch: B.Tech AIML – B
Roll no: 97
Output:
Output:
$strings
[1] "apple" "banana" "cherry"
$numbers
[1] 10 20 30
$vectors
$vectors[[1]]
[1] 1 2 3
$vectors[[2]]
[1] 4 5 6
$logical_values
[1] TRUE FALSE TRUE
[1] "Strings: apple" "Strings: banana" "Strings: cherry"
[1] "First number: 10"
[1] "Second vector: 4" "Second vector: 5" "Second vector: 6"
[1] "Logical values: TRUE" "Logical values: FALSE"
[3] "Logical values: TRUE"
Experiment No 6
Name: Tohidealam Firoj Nadaf
Branch: B.Tech AIML – B
Roll no: 97
Output:
print("Matrix 1:")
print(matrix1)
print("Matrix 2:")
print(matrix2)
print("Sum of Matrices:")
print(sum_matrix)
print("Difference of Matrices:")
print(diff_matrix)
Output:
print("Factor:")
print(data_factor)
print("Reordered Factor:")
print(data_factor)
Output:
[1] "Factor:"
[1] High Medium High Low Medium High Low Low
Levels: Low < Medium < High
[1] "Levels:"
[1] "Low" "Medium" "High"
[1] "Table:"
data_factor
Low Medium High
3 2 3
Output:
Output: