evoalgs-r-practise/operators.R
2024-04-30 10:03:37 +02:00

104 lines
1.5 KiB
R

# Addition
result <- 10 + 5
print(result) # 15
# Subtraction
result <- 10 - 5
print(result) # 5
# Multiplication
result <- 10 * 5
print(result) # 50
# Division
result <- 10 / 5
print(result) # 2
result <- 10 / 3
print(result) # 3.333333
# Exponentiation
result <- 10 ^ 2
print(result) # 100
# Integer Division
result <- 10 %/% 3
print(result) # 3
# Modulus
result <- 10 %% 3
print(result) # 1
# AND
result <- TRUE & FALSE
print(result) # FALSE
# OR
result <- TRUE | FALSE
print(result) # TRUE
# NOT
result <- !TRUE
print(result) # FALSE
# Greater than
result <- 10 > 5
print(result) # TRUE
# Less than
result <- 10 < 5
print(result) # FALSE
# Equal to
result <- 10 == 10
print(result) # TRUE
# Not equal to
result <- 10 != 5
print(result) # TRUE
# Greater than or equal to
result <- 10 >= 10
print(result) # TRUE
# Less than or equal to
result <- 10 <= 5
print(result) # FALSE
# Basic assignment
x <- 10
print(x) # 10
# Alternate assignment (less common in R)
x = 20
print(x) # 20
# Increment and assign (not directly supported like in other languages, shown with equivalent R code)
x <- x + 5
print(x) # 25
# Decrement and assign (equivalent R code)
x <- x - 5
print(x) # 20
# Multiply and assign (equivalent R code)
x <- x * 2
print(x) # 40
# Divide and assign (equivalent R code)
x <- x / 2
print(x) # 20
# Element-wise multiplication of vectors
v1 <- c(1, 2, 3)
v2 <- c(4, 5, 6)
result <- v1 * v2
print(result) # c(4, 10, 18)
# Element-wise comparison of vectors
result <- v1 > 2
print(result) # c(FALSE, FALSE, TRUE)