SML Practical 2: Overview of Operators in R
SML Practical 2: Overview of Operators in R
Practical 2
Aim-
THEORY
In R, numbers can be represented in various types, such as numeric, integer, and complex.
Each type has its specific use and characteristics:
Numeric:
The default type for numbers in R.
Can represent both integer and floating-point numbers.
Examples: 4, 3.14.
Integer:
Specifically represents whole numbers.
In R, you can explicitly create an integer using the L suffix (e.g., 5L).
Useful when you want to work with whole numbers only.
Complex:
Represents complex numbers, which include a real and an imaginary part.
In R, a complex number is created using the i notation for the imaginary part (e.g., 3 + 2i).
Overview of Operators in R
Operators in R are represented by specific symbols that denote various operations such as arithmetic
calculations, comparisons, logical evaluations, and data manipulation tasks. Understanding these
symbols and their associated operations is key to effective R programming.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations on numerical data.
2. Relational Operators
Relational operators compare two values or expressions and return a logical value (TRUE or FALSE).
Greater than (>): Checks if the left value is greater than the right.
Less than (<): Checks if the left value is less than the right.
Greater than or equal to (>=): Checks if the left value is greater than or equal to the right.
Less than or equal to (<=): Checks if the left value is less than or equal to the right.
Equal to (==): Checks if the two values are equal.
Not equal to (!=): Checks if the two values are not equal.
3. Logical Operators
Logical operators are used to combine or negate logical conditions.
4. Assignment Operators
Assignment operators are used to assign values to variables.
Left Assignment (<-): Assigns the value on the right to the variable on the left.
Right Assignment (->): Assigns the value on the left to the variable on the right.
Equal Sign Assignment (=): Another form of left assignment, commonly used in function arguments.
program
# Numeric
a <- 4.56
print(a) # Print numeric value
# Integer
b <- 5L
print(b) # Print integer value
# Complex
c <- 3 + 2i
print(c) # Print complex value
R-Numbers:
a: A numeric value 4.56.
b: An integer value 5L.
c: A complex number 3 + 2i.
Conversions:
a_to_integer: Converts a from numeric to integer.
b_to_numeric: Converts b from integer to numeric.
a_to_complex: Converts a from numeric to complex.
Operations:
Addition (sum_ab): Adds a and b.
Subtraction (diff_ab): Subtracts b from a.
Square Root (sqrt_a): Calculates the square root of a.
Expected Output
[1] 4.56
[1] 5
[1] 3+2i
[1] 4
[1] 5
[1] 4.56+0i
[1] 9.56
[1] -0.44
[1] 2.135416
Explanation of Output
The program demonstrates the use of basic data types in R, converts between them, and performs
simple arithmetic operations.
The values printed correspond to the results of each operation and conversion as expected.
conclusion
Understanding Data Types: Numeric, integer, and complex data types are foundational in R for
mathematical and statistical operations.
Function Basics: Knowing how to use functions like print(), as.integer(), as.numeric(), as.complex(),
and sqrt() is crucial for performing
conversions and calculations.
Arithmetic Operations: Basic arithmetic operations (addition, subtraction) are straightforward and
essential for manipulating numeric data.