Vector Functions
In this page:
Vector Functions
R provides many built-in functions that operate on an entire vector at once, like sum(), length(), max(), min(), and sort(). These are typically implemented in optimized C code and run much faster than an equivalent manual loop. Learning these functions is key to writing concise, idiomatic R.
Note: Reach for a built-in vector function like sum() or sort() before writing a manual for-loop -- one almost always exists.
Example: Vector Functions
v <- c(5, 3, 8, 1, 9)
cat("Sum:", sum(v), "\n")
cat("Max:", max(v), "\n")
cat("Min:", min(v), "\n")
print(sort(v))
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: