R Program to Count the Number of Elements in a Vector
Counts elements using length.
[1] 4
Master vectors: element-wise operations, membership tests, counts, indexing, sorting, min/max, and formatting. Core building blocks for data analysis.
Counts elements using length.
[1] 4
Adds two numeric vectors element-wise.
[1] 5 7 9
Computes sum, mean, and product of a numeric vector.
Sum: 10 Mean: 2.5 Product: 24
Uses min() and max() on a numeric vector.
Min: 1 Max: 8
Sorts a numeric vector in ascending order.
[1] 1 3 5 8
Sums the first N natural numbers.
[1] 5050
Sums 1..N recursively.
[1] 55
Tests membership using %in%.
[1] FALSE
Finds index using which.
[1] 2
Accesses elements via indexing.
[1] 10 30
Computes mode via table and which.max.
[1] 3
Formats integers with fixed width using sprintf.
[1] "005" "042" "300"
main.R, then run Rscript main.R from your terminal. Alternatively, use an IDE such as RStudio.
R is a powerful language for data analysis, visualization, and statistical computing. Practicing small, focused programs builds fluency with vectors, data frames, lists, functions, control flow, and string manipulation.
On this page, you’ll find clean, well-formatted examples that run across platforms. Each example includes a short description and expected output so you can quickly verify your understanding. Explore the topics via the sidebar and extend the code to experiment further.
Whether you’re preparing for coursework, handling data tasks at work, or learning R for research, these examples offer a practical path to mastery. Bookmark the page and revisit to keep sharpening your skills.