# R Fundamentals

# 1. Calculations

2 + 3
100-54
10 / 4
2^3
(2 + 3) * 4
16 %% 5

# Operators: +, -, *, /, ^, %%. Parentheses control the order of operations.
# A comment starts with #. R ignores comments when running the script.

# 2. Variables

temperature_c <- 20  # degrees Celsius
temperature_f <- temperature_c * 9 / 5 + 32

# <- assigns a value. Names are case-sensitive.
# A stored answer does not update automatically when an input changes.

temperature_c <- 25
temperature_f  # still 68

temperature_f <- temperature_c * 9 / 5 + 32
temperature_f  # now 77

class(temperature_c)  # numeric
class("temperature")  # character
class(TRUE)           # logical
class(46L)            # integer

# 3. Vectors

# A basic vector holds values of the same type.
# These are made-up temperature readings from hours 8 through 12.

temperatures_c <- c(18, 20, 23, 25, 22)
hours <- 8:12

temperatures_c
hours
length(temperatures_c)

# Square brackets select values. R starts counting positions at 1.

temperatures_c[1]
temperatures_c[2:4]
temperatures_c[c(1, 3, 5)]
hours[1]  # position 1 contains hour 8

head(temperatures_c, 2)
tail(temperatures_c, 2)

1:5
seq(from = 0, to = 2, by = 0.5)

# Vectorized arithmetic: convert every reading at once.

temperatures_f <- temperatures_c * 9 / 5 + 32
round(temperatures_f, digits = 1)

# round() displays a rounded copy; temperatures_f still holds its original
# values. To store the rounded values, assign the result to an object.

mean(temperatures_c)  # 21.6
min(temperatures_c)   # 18
max(temperatures_c)   # 25
sum(temperatures_c)   # valid arithmetic, but not a useful temperature

# 4. Graphs

# Horizontal coordinates first, vertical coordinates second.
# Label the axes and include units. pch = 16 selects filled circles.

plot(hours, temperatures_c,
     pch = 16,
     xlab = "Hour of day",
     ylab = "Temperature (degrees C)",
     main = "Temperature readings")

# type = "o" adds lines through the points.
# The lines show order; they are not additional measurements.

plot(hours, temperatures_c,
     type = "o", pch = 16,
     xlab = "Hour of day",
     ylab = "Temperature (degrees C)")

# 5. Loops

# A for loop repeats instructions for each value.
# Use print() to display results from inside a loop.

for (reading in temperatures_c) {
  print(reading)
}

# To save results, create a vector and fill its positions.
# numeric() creates zeros; seq_along() supplies the valid positions.
# The first loop visits values; this loop visits positions (1 through 5).

converted_f <- numeric(length(temperatures_c))

for (i in seq_along(temperatures_c)) {
  converted_f[i] <- temperatures_c[i] * 9 / 5 + 32
}

converted_f
# Check that the loop agrees with the vectorized calculation.
# all.equal() allows for small floating-point differences.
isTRUE(all.equal(converted_f, temperatures_f))  # TRUE

# 6. Comparisons and decisions

# Comparisons: ==, !=, <, <=, >, >=.
# == tests equality; <- assigns a value.

temperatures_c > 22
hours == 10

# Logical indexing selects the entries where the condition is TRUE.

temperatures_c[temperatures_c > 22]
hours[temperatures_c > 22]  # hours 10 and 11
sum(temperatures_c > 22)   # count matches: TRUE counts as 1, FALSE as 0

# if needs one TRUE or FALSE, not a whole vector of comparisons.

latest_reading <- tail(temperatures_c, 1)

# The latest reading is exactly 22, so the else branch runs: > is not >=.
if (latest_reading > 22) {
  print("The latest reading is above 22 degrees C.")
} else {
  print("The latest reading is at or below 22 degrees C.")
}

# any() and all() turn multiple comparisons into one logical value.

any(temperatures_c > 22)
all(temperatures_c > 22)

if (any(temperatures_c > 22)) {
  print("At least one reading exceeds 22 degrees C.")
}