Vector Operations and Utilities

Master vectors: element-wise operations, membership tests, counts, indexing, sorting, min/max, and formatting. Core building blocks for data analysis.

R Program to Add Leading Zeros to Vector

Program (R)
Example & Expected Output

Formats integers with fixed width using sprintf.

[1] "005" "042" "300"

R Program to Add Two Vectors

Program (R)
Example & Expected Output

Adds two numeric vectors element-wise.

[1] 5 7 9

Find Sum, Mean and Product of Vector in R Programming

Program (R)
Example & Expected Output

Computes sum, mean, and product of a numeric vector.

Sum: 10
Mean: 2.5
Product: 24

R Program to Find Minimum and Maximum

Program (R)
Example & Expected Output

Uses min() and max() on a numeric vector.

Min: 1
Max: 8

R Program to Sort a Vector

Program (R)
Example & Expected Output

Sorts a numeric vector in ascending order.

[1] 1 3 5 8

R Program to Find the Sum of Natural Numbers

Program (R)
Example & Expected Output

Sums the first N natural numbers.

[1] 5050

Sum of Natural Numbers Using Recursion

Program (R)
Example & Expected Output

Sums 1..N recursively.

[1] 55

R Program to Check if a Vector Contains the Given Element

Program (R)
Example & Expected Output

Tests membership using %in%.

[1] FALSE

R Program to Count the Number of Elements in a Vector

Program (R)
Example & Expected Output

Counts elements using length.

[1] 4

R Program to Find Index of an Element in a Vector

Program (R)
Example & Expected Output

Finds index using which.

[1] 2

R Program to Access Values in a Vector

Program (R)
Example & Expected Output

Accesses elements via indexing.

[1] 10 30

R Program to Find the Statistical Mode

Program (R)
Example & Expected Output

Computes mode via table and which.max.

[1] 3

Frequently Asked Questions

Install R (from CRAN). Save the code in a file like main.R, then run Rscript main.R from your terminal. Alternatively, use an IDE such as RStudio.

No. All examples rely on base R functions to ensure compatibility across systems. Where packages are useful, they’ll be explicitly mentioned.

Yes. Copy the code, tweak inputs, and observe outputs. Experimentation is the fastest way to build intuition and mastery.

Yes. R is cross-platform. The examples run on Windows, macOS, and Linux with a standard R installation.

Learn R the Practical Way

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.