Ex No.2
Ex No.2
AIM: To Create R program for the concepts such as data types, operators, matrices,
lists and functions
Algorithm:
Program:
# Data Types
# Numeric
x <- 5
y <- 3.14
# Character
name <- "John"
# Logical (Boolean)
is_raining <- TRUE
# Integer
age <- as.integer(30)
# Complex
complex_number <- 2 + 3i
# Factor
gender <- factor("Male", levels = c("Male", "Female"))
# Operators
# Arithmetic Operators
sum_result <- x + y
product_result <- x * y
difference_result <- x - y
quotient_result <- x / y
modulo_result <- x %% 2
exponentiation_result <- x^2
# Comparison Operators
is_greater <- x > y
is_equal <- x == y
is_not_equal <- x != y
# Logical Operators
logical_and <- is_raining && is_greater
logical_or <- is_raining || is_greater
logical_not <- !is_raining
# Assignment Operator
z <- 10
# Concatenation Operator
full_name <- paste("John", "Doe")
# Matrices
# Create a 3x3 matrix
matrix_data <- matrix(1:9, nrow = 3, ncol = 3)
# Functions
# Define a function to calculate the square of a number
square <- function(x) {
result <- x^2
return(result)
}
# Print Results
print("Data Types:")
print(x)
print(y)
print(name)
print(is_raining)
print(age)
print(complex_number)
print(gender)
print("Operators:")
print(sum_result)
print(product_result)
print(difference_result)
print(quotient_result)
print(modulo_result)
print(exponentiation_result)
print(is_greater)
print(is_equal)
print(is_not_equal)
print(logical_and)
print(logical_or)
print(logical_not)
print(z)
print(full_name)
print("Matrices:")
print(matrix_data)
print(element)
print("Lists:")
print(my_list)
print(list_element)
print("Functions:")
print(squared_value)
Output:
Result : The above program is executed successfully