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

Ex No.2

The document describes a R program that demonstrates various R programming concepts like data types, operators, matrices, lists, and functions. The program defines variables of different data types, performs arithmetic, comparison, and logical operations on values. It creates a 3x3 numeric matrix and accesses elements, makes a list with different data types and accesses elements. A custom square function is defined to calculate the square of a number. Finally, it prints the results.
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 views5 pages

Ex No.2

The document describes a R program that demonstrates various R programming concepts like data types, operators, matrices, lists, and functions. The program defines variables of different data types, performs arithmetic, comparison, and logical operations on values. It creates a 3x3 numeric matrix and accesses elements, makes a list with different data types and accesses elements. A custom square function is defined to calculate the square of a number. Finally, it prints the results.
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
You are on page 1/ 5

EX NO:1

AIM: To Create R program for the concepts such as data types, operators, matrices,
lists and functions

Algorithm:

Step 1: Data Types:


 Declare and assign values to various data types, including numeric, character,
logical, integer, complex, and factor variables.
Step 2: Operators:
 Perform arithmetic operations (addition, multiplication, subtraction, division,
modulo, exponentiation).
 Use comparison operators (greater than, equal to, not equal to).
 Apply logical operators (AND, OR, NOT).
 Assign values using the assignment operator.
 Concatenate strings using the concatenation operator.
Step 3: Matrices:
 Create a 3x3 matrix using the matrix() function with numeric data.
 Access elements in the matrix using indexing.
Step 4: Lists:
 Create a list with elements of different data types, including character,
numeric, and a vector.
 Access list elements using indexing.
Step 5: Functions:
 Define a custom function named square() that calculates the square of a
given number.
 Call the square() function with an argument to calculate the square.
Step 6: Print Results:
 Print the values of various variables, results of operations, and elements of
data structures to the console.

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)

# Access elements in the matrix


element <- matrix_data[2, 3]
# Lists
# Create a list
my_list <- list(name = "John", age = 30, hobbies = c("Reading", "Gardening"))

# Access list elements


list_element <- my_list$name

# Functions
# Define a function to calculate the square of a number
square <- function(x) {
result <- x^2
return(result)
}

# Call the function


squared_value <- square(4)

# 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

You might also like