Powered by AI
Copilot
Certainly! Let’s explore arithmetic and boolean operators in R programming with examples:
1. Arithmetic Operators:
These operators allow us to perform mathematical calculations.
Here are the basic arithmetic operators in R:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
^ or ** (Exponentiation)
%% (Modulus)
%/% (Integer division)
%*% (Matrix multiplication)
%o% (Outer product)
%x% (Kronecker product)
Examples:
# Basic operations
3 + 5 # Output: 8
8 - 3 # Output: 5
7 * 5 # Output: 35
1 / 2 # Output: 0.5
4 ^ 4 # Output: 256
5 %% 3 # Output: 2
5 %/% 3 # Output: 1
# Vector operations
x <- c(1, 7, 3)
y <- c(9, 4, 5)
x + y # Output: 10 11 8
x * y # Output: 9 28 15
# Matrix operations
s <- matrix(1:6, nrow = 2, ncol = 3)
t <- matrix(8:13, nrow = 2, ncol = 3)
s + t # Output: 9 13 17; 11 15 19
s * t # Output: 8 30 60; 18 44 78
2. Boolean Operators:
These operators evaluate expressions to either TRUE or FALSE.
Common boolean operators:
== (Equality)
!= (Inequality)
< (Less than)
> (Greater than)
<= (Less than or equal to)
>= (Greater than or equal to)
! (Negation)
& (Logical AND)
| (Logical OR)
Example:
# Boolean expressions
a <- 10
b <- 5
a == b # Output: FALSE
a > b # Output: TRUE
!(a < b) # Output: TRUE
a <= b # Output: FALSE
Feel free to explore these operators further or ask for more examples! 😊