# Simple Assignment using (<-).
num <- 20
print(num)[1] 20
Mugabi Trevor .L
2024-09-03
Assigment Operator, Logical operator, Arithmetic operator, Comparison operator
The types of operations in R can be broken down into these below: 1. Assignment Operators 1. Arithmetic Operators 1. Comparison Operators 1. Logical Operators 1. Identity Operators 1. Membership Operators
Lets Explore one by one below to see exactly what each is all about and some examples along the way.
Assignment operators in programming are used to assign values to variables. These operators store a value on the right side of the operator into the variable on the left side. In most programming languages, assignment operators not only assign values but also can perform arithmetic operations and then assign the result to a variable.
NB: If you coming from another Programming language. its time to let you know that R doesnt have short hand syntax support so we can not use syntax like num = 1 then num +=1
Arithmetic operations in programming and mathematics involve performing basic calculations on numerical values. These operations are fundamental to many algorithms and data manipulations. Here’s a detailed definition and explanation of each.
Arithmetic Operations: 1. Addition (+): Combines two values (e.g., a + b). 1. Subtraction (-): Subtracts one value from another (e.g., a - b). 1. **Multiplication (*): Multiplies two values (e.g., a * b). 1. Division (/): Divides one value by another (e.g., a / b). 1. Modulus (%%): Returns the remainder of division (e.g., a % b). 1. Exponentiation (^): Raises a value to the power of another (e.g., a b).
Comparison operators in programming are used to compare two values or expressions. These operators return a logical value (TRUE or FALSE) depending on whether the comparison is true or false. They are fundamental in decision-making structures like if statements, loops, and other control flow mechanisms.
Logical operators are used in programming to perform logical operations, typically on boolean values (TRUE or FALSE). These operators are essential for decision-making and control flow in code, allowing you to combine and evaluate conditions.
In R, identity operators are used to test if two objects are the same object or have the same identity. This concept is different from equality operators, which test if two objects are equivalent in value. Identity operators are particularly useful for checking if two variables point to the same object in memory, which is more about object identity rather than object equality. ### Types of Identity Operators 1. Identical() Function: Tests if two R objects are exactly the same, both in terms of their value and attributes. It checks for identity, meaning that the two objects are considered the same if they have the same type, structure, and content.
In R, membership operators are used to test if elements belong to a specific set or structure. They are useful for checking whether values are present within vectors, lists, or other data structures. Here are the main membership operators in R:
%in% Operator: Tests if elements of one vector are present in another vector. This operator is used to check membership within a set.
match() Function: Finds the positions of the first matches of a vector of elements in another vector. It returns the index positions where matches are found, or NA if no match is found.
%in% vs. match(): %in% Operator: Checks if elements are present in a set and returns a logical vector. match() Function: Provides the position of the matches or NA if no match is found.
Summary: %in% is used for membership testing, checking if elements are present in a set, and returns a logical vector and match() is used to find the index positions of matches and returns those positions or NA.
Both operators and functions are essential for data manipulation and querying in R, allowing you to efficiently handle and analyze your data.
# Using %in%.
# Defining vectors.
fruits <- c("apple", "banana", "cherry")
my_fruit <- "banana"
my_fruits <- c("apple", "orange")
# Checking if `my_fruit` is in `fruits`
my_fruit %in% fruits # TRUE
# Checking if elements in `my_fruits` are in `fruits`
my_fruits %in% fruits # TRUE FALSE
# Here, "banana" is in the fruits vector, so the result is TRUE. The vector my_fruits
# contains "apple" (which is in fruits) and "orange" (which is not), so the result is TRUE FALSE.# Using match(x, table)
# Defining vectors
fruits <- c("apple", "banana", "cherry")
my_fruits <- c("banana", "cherry", "orange")
# Find positions of `my_fruits` in `fruits`
match(my_fruits, fruits) # 2 3 NA
#Here, "banana" is the 2nd element in fruits,
#"cherry" is the 3rd, and "orange" is not found, so it returns NA.Description: This exercise will help you practice using logical operators in R to evaluate conditions and filter data based on multiple criteria. You will be working directly with vectors and logical statements.
Given two integers x and y, evaluate the following conditions: x <- 12, y <- -8
Are both x and y positive?
Use x > 0 & y > 0 to check if both are positive.
Is at least one of x or y negative?
Use x < 0 | y < 0 to check if at least one is negative.
Are both x and y even?
Use (x %% 2 == 0) & (y %% 2 == 0) to check if both are even.
Is at least one of x or y odd?
Use (x %% 2 != 0) | (y %% 2 != 0) to check if at least one is odd.
Run each block of code in R. Understand how logical operators are used to evaluate and filter data based on multiple conditions. This exercise will help you gain a solid understanding of logical operators in R by applying them directly to vectors and conditions.