# Arithmetic Operators in R: Range, Addition, and Vector Operations
# In programming with R, understanding arithmetic operators is crucial for effective data manipulation and mathematical operations. My short project focuses on the core arithmetic concepts such as range and addition, basic addition and subtraction with single numbers, and handling operations within vectors. Additionally, my project handles recycling and behavior of special values like NA and NaN when performing tasks.
# 
# Range and Addition
# The : operator in R is frequently used to create a sequence or range of numbers, for example, 1:5 produces a vector [1, 2, 3, 4, 5]. An important detail in R’s operation precedence is that : has higher precedence than +, which can lead to unexpected outcomes if not properly ordered.
# 
# For instance, 3 + 1:5 evaluates as 3 + c(1, 2, 3, 4, 5), yielding [4, 5, 6, 7, 8]. To control the order, parentheses are required: (3 + 1):5 will evaluate the addition first, resulting in a more intuitive output [4, 5].
# Addition and Subtraction
# Basic arithmetic in R is straightforward with numbers or vectors. Single-number addition or subtraction is performed using + and -:


3 + 4.5 
## [1] 7.5
3 + NA 
## [1] NA
# The handling of NA and NaN is also significant in arithmetic. Operations involving these special values yield NA or NaN, indicating missing or undefined values:

NaN - NA 
## [1] NaN
# When working with variables, assigning numbers to variables and performing arithmetic operations is equally straightforward. For example:

a <- 3; B <- 4.5; 
cc <- 2; Dd <- 3.8;
a + B + cc - Dd 
## [1] 5.7
# Vector Operations
# R’s vectorized nature makes arithmetic with vectors intuitive. When adding or subtracting a constant to/from a vector, each element in the vector is adjusted by that constant. For example:

A <- c(3, 4.5, 2, -3.8)
A + 2 
## [1]  5.0  6.5  4.0 -1.8
8 - A 
## [1]  5.0  3.5  6.0 11.8
# We can also add vectors element-wise or use a specific index range. For example:


A[1:2] + 3 
## [1] 6.0 7.5
A[1:2] - A[3:4] 
## [1] 1.0 8.3
# Using the sum function, all vector elements can be summed together:

sum(A) 
## [1] 5.7
# Recycling in R
# R has a unique feature called recycling that applies when performing operations on vectors of unequal length. Shorter vectors repeat to match the length of the longest vector:


# B <- c(3, 5, -3, 2.7, 1.8)
# A + B 
# # Warning: longer object length is not a multiple of shorter object length[1]  6.0  9.5 -1.0 -1.1  4.8
# I had to comment out the above code because I was getting an error message, So to resolve the recycling warning in R when adding vectors of unequal lengths, it was important for myself to ensure that the vectors have the same length or to adjust the operation to match lengths. The warning arises because R recycles elements of the shorter vector to match the length of the longer vector, which can lead to unintended results.
# 
# One solution is to modify the shorter vector, A, by adding an additional element so that it matches the length of B. For example, if B has five elements, adding an extra element (e.g., 0) to A creates a vector of the same length, allowing for element-wise addition without warnings.

# Original vectors
#A <- c(3, 4.5, 2, -3.8) # 4 elements
#B <- c(3, 5, -3, 2.7, 1.8) # 5 elements

# Adjust A to have 5 elements but I still received the warning message so I will revisit this later on: 
# Warning: longer object length is not a multiple of shorter object length[1]  6.0  9.5 -1.0 -1.1  1.8  3.0
A <- c(A, 0) 
A + B 
## [1] 7.5 9.0 6.5 0.7 4.5
# To avoid recycling warnings, use only elements matching the shortest vector:


B[1:length(A)] + A # Results in element-wise addition without recycling
## [1] 7.5  NA  NA  NA  NA
# Now it is time to load the libraries since I have already installed the packages 
library(knitr)
library(kableExtra)

# Here is a data table to show the results in a tabular form
table_data <- data.frame(
  Operation = c("Basic Addition", "Addition with Range", "Controlled Order",
                "Addition with NA", "Vector Addition", "Sum of Vector",
                "Recycling", "Avoid Recycling"),
  Code_Example = c("3 + 4.5", "3 + 1:5", "(3 + 1):5", "3 + NA",
                   "A + 2 (where A = c(3, 4.5, 2, -3.8))", "sum(A)",
                   "A + B (where A shorter)", "B[1:length(A)] + A"),
  Result = c("[1] 7.5", "[1] 4 5 6 7 8", "[1] 4 5", "[1] NA",
             "[1] 5.0 6.5 4.0 -1.8", "[1] 5.7", 
             "Warning; [1] 6.0 9.5 -1.0 -1.1 4.8", "[1] 6.0 9.5 -1.0 -1.1")
)

# Here I will add some color to the table 
kable(table_data, "html", col.names = c("Operation", "Code Example", "Result")) %>%
  kable_styling(full_width = F, bootstrap_options = c("striped", "hover", "condensed")) %>%
  column_spec(1, bold = TRUE, color = "white", background = "#4CAF50") %>%
  column_spec(2, background = "#E0F7FA") %>%
  column_spec(3, background = "#FFEBEE")
Operation Code Example Result
Basic Addition 3 + 4.5 [1] 7.5
Addition with Range 3 + 1:5 [1] 4 5 6 7 8
Controlled Order (3 + 1):5 [1] 4 5
Addition with NA 3 + NA [1] NA
Vector Addition A + 2 (where A = c(3, 4.5, 2, -3.8)) [1] 5.0 6.5 4.0 -1.8
Sum of Vector sum(A) [1] 5.7
Recycling A + B (where A shorter) Warning; [1] 6.0 9.5 -1.0 -1.1 4.8
Avoid Recycling B[1:length(A)] + A [1] 6.0 9.5 -1.0 -1.1