Write R code to create a numeric vector called temps with values 23, 27, 19, 21. Then create a new vector called temps_celsius by subtracting 2 from each element of temps.
Vectors are one of the most fundamental data structures in R. They are sequences of elements of the same type (numeric, character, or logical).
Use the c()
function to create a vector:
numbers <- c(2, 4, 6, 8)
names <- c("Alice", "Bob", "Charlie")
flags <- c(TRUE, FALSE, TRUE)
R allows operations to be applied to all elements of a vector at once:
x <- c(1, 2, 3, 4)
y <- c(5, 6, 7, 8)
x + y # Adds corresponding elements
x * 2 # Multiplies each element by 2
Vectorized operations make R extremely efficient for handling data.