Vector Operations and Utilities

Key takeaway: Practice R with 53+ runnable code examples — vectors, strings, math, data frames, recursion, and sampling — with expected output to verify instantly.

R is a programming language for statistical computing and data analysis. These examples are for data science students, researchers, analysts, and anyone learning R for coursework or work projects.

Last updated: October 2026 · 53 examples · All platforms

Quick Start

  1. Select a topic from the sidebar or mobile menu
  2. Study the code and expected output
  3. Run instantly in our Online R Compiler & Editor or paste into RStudio
  4. Modify inputs to experiment and learn

Interactive Tool:Online R Compiler & Editor · Related:All R Examples · Python Examples · Java Examples · C Examples

R Program to Check if a Vector Contains the Given Element

Tests membership using %in%.

Program (R)
v <- c(10, 20, 30)
print(25 %in% v)
Expected Output
[1] FALSE

R Program to Add Two Vectors

Adds two numeric vectors element-wise.

Program (R)
a <- c(1, 2, 3)
b <- c(4, 5, 6)
print(a + b)
Expected Output
[1] 5 7 9

Find Sum, Mean and Product of Vector in R Programming

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

Program (R)
x <- c(1, 2, 3, 4)
cat("Sum:", sum(x), "\n")
cat("Mean:", mean(x), "\n")
cat("Product:", prod(x), "\n")
Expected Output
Sum: 10
Mean: 2.5
Product: 24

R Program to Find Minimum and Maximum

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

Program (R)
x <- c(5, 3, 8, 1)
cat("Min:", min(x), "\n")
cat("Max:", max(x), "\n")
Expected Output
Min: 1
Max: 8

R Program to Sort a Vector

Sorts a numeric vector in ascending order.

Program (R)
x <- c(5, 3, 8, 1)
print(sort(x))
Expected Output
[1] 1 3 5 8

R Program to Find the Sum of Natural Numbers

Sums the first N natural numbers.

Program (R)
n <- 100
print(sum(1:n))
Expected Output
[1] 5050

Sum of Natural Numbers Using Recursion

Sums 1..N recursively.

Program (R)
sumNat <- function(n) if (n <= 1) n else n + sumNat(n-1)
print(sumNat(10))
Expected Output
[1] 55

R Program to Count the Number of Elements in a Vector

Counts elements using length.

Program (R)
v <- c(10, 20, 30, 40)
print(length(v))
Expected Output
[1] 4

R Program to Find Index of an Element in a Vector

Finds index using which.

Program (R)
v <- c(10, 20, 30)
print(which(v == 20))
Expected Output
[1] 2

R Program to Access Values in a Vector

Accesses elements via indexing.

Program (R)
v <- c(10, 20, 30)
print(v[c(1,3)])
Expected Output
[1] 10 30

R Program to Find the Statistical Mode

Computes mode via table and which.max.

Program (R)
x <- c(1,2,2,3,3,3,4)
tbl <- table(x)
print(as.integer(names(tbl)[which.max(tbl)]))
Expected Output
[1] 3

R Program to Add Leading Zeros to Vector

Formats integers with fixed width using sprintf.

Program (R)
x <- c(5, 42, 300)
print(sprintf("%03d", x))
Expected Output
[1] "005" "042" "300"

Frequently Asked Questions

Use Rscript from your terminal. Install R from CRAN, save as main.R, run Rscript main.R. Or use RStudio.

No, base R only. All examples use built-in functions for maximum portability.

Yes, absolutely. Copy, tweak inputs, observe outputs — experimentation is the fastest way to learn.

Yes, fully cross-platform. R runs on Windows, macOS, and Linux. Works with R 3.6+.

Learn R the Practical Way

R ranks among the top 20 languages on the TIOBE Index, dominant in academia and data science.

Topics Covered

  • Vectors, lists, and data frames
  • String manipulation and text processing
  • Math, statistics, and random sampling
  • Control flow — if/else, for, while
  • Recursion and custom functions
  • File I/O and formatted output

Who Is This For?

  • Data science students — coursework and R-based exams
  • Researchers — statistical analysis for papers
  • Analysts — data wrangling skills for work
  • Career switchers — transitioning into data roles

Practice Online:Online R Compiler & Editor (/tools/r-compiler-editor) · Related:All R Examples · Python Examples