0% found this document useful (0 votes)
15 views

SML Practical 2: Overview of Operators in R

Sml
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)
15 views

SML Practical 2: Overview of Operators in R

Sml
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

SML

Practical 2

Aim-

Write Program to-


a. Demonstrate the use of R-Numbers. (numeric, integer, complex).
b. Convert number from one type to other using functions.
c. Perform following operations.
i. Addition and Subtraction on numbers.
ii. Find Square root using of number using built-in function.

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.

Addition (+): Adds two numbers.


Subtraction (-): Subtracts one number from another.
Multiplication (*): Multiplies two numbers.
Division (/): Divides one number by another.
Exponentiation (^): Raises a number to the power of another.
Modulus (%%): Returns the remainder after division of one number by another.
Integer Division (%/%): Divides one number by another and returns the integer part of the result.

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.

AND (&): Returns TRUE if both conditions are true.


OR (|): Returns TRUE if at least one condition is true.
NOT (!): Inverts the logical value; TRUE becomes FALSE and vice versa.

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

# a. Demonstrate the use of R-Numbers (numeric, integer, complex)

# 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

# b. Convert numbers from one type to another

# Convert numeric to integer


a_to_integer <- as.integer(a)
print(a_to_integer) # Print converted integer

# Convert integer to numeric


b_to_numeric <- as.numeric(b)
print(b_to_numeric) # Print converted numeric

# Convert numeric to complex


a_to_complex <- as.complex(a)
print(a_to_complex) # Print converted complex
# c. Perform the following operations

# i. Addition and Subtraction on numbers


sum_ab <- a + b
diff_ab <- a - b
print(sum_ab) # Print result of addition
print(diff_ab) # Print result of subtraction

# ii. Find square root of a number using built-in function


sqrt_a <- sqrt(a)
print(sqrt_a) # Print square root of the numeric value

Explanation of the Program

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.

NOTE -Attach proper prints of input and output

You might also like