Data Frame Operations and Manipulation 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
Jump to Topic: R "Hello World" Program R Program to Add Two Vectors Find Sum, Mean and Product of Vector in R Programming R Program to Take Input From User R Program to Generate Random Number from Standard Distributions R Program to Sample from a Population R Program to Find Minimum and Maximum R Program to Sort a Vector R Program to Find the Factorial of a Number R Multiplication Table R Program to Check Prime Number R Program to check Armstrong Number R Program to Print the Fibonacci Sequence R Program to Check for Leap Year Check if a Number is Odd or Even in R Programming R Program to Check if a Number is Positive, Negative or Zero R Program to Find the Sum of Natural Numbers Convert Decimal into Binary using Recursion in R R program to Find the Factorial of a Number Using Recursion R Program to Find the Factors of a Number Fibonacci Sequence Using Recursion in R R Program to Find H.C.F. or G.C.D. R Program to Find L.C.M. R Program to Make a Simple Calculator Sum of Natural Numbers Using Recursion R Program to Concatenate Two Strings R Program to Find the Length of a String R Program to Check if Characters are Present in a String R Program to Extract n Characters From a String R Program to Replace Characters in a String R Program to Compare two Strings R Program to Convert Factors to Characters R Program to Convert a List to a Dataframe R Program to Create an Empty Dataframe R Program to Combine Two Dataframe into One R Program to Change Column Name of a Dataframe R Program to Extract Columns From a Dataframe R Program to Drop Columns in a Dataframe R Program to Reorder Columns in a Dataframe R Program to Split Dataframe R Program to Merge Multiple Dataframes R Program to Delete Rows From Dataframe R Program to Make a List of Dataframes R Program to Concatenate a Vector of Strings R Program to Check if a Vector Contains the Given Element R Program to Count the Number of Elements in a Vector R Program to Find Index of an Element in a Vector R Program to Access Values in a Vector R Program to Find the Statistical Mode R Program to Add Leading Zeros to Vector R Program to Write to a File R Program to Trim Leading and Trailing Whitespaces R Program to Format Decimal Places
R Program to Create an Empty Dataframe Copy Code
Defines a data frame with no rows.
Program (R)
df <- data.frame(id=integer(), name=character(), stringsAsFactors=FALSE)
print(df) Expected Output Copy Output
[1] id name
<0 rows> (or 0-length row.names) R Program to Convert a List to a Dataframe Copy Code
Converts a named list into a data frame.
Program (R)
l <- list(name=c("Ann","Bob"), age=c(19,21))
df <- as.data.frame(l)
print(df) Expected Output Copy Output
name age
1 Ann 19
2 Bob 21 R Program to Combine Two Dataframe into One Copy Code
Stacks data frames with same columns using rbind.
Program (R)
df1 <- data.frame(id=c(1,2), name=c("Ann","Bob"))
df2 <- data.frame(id=c(3), name=c("Cara"))
df <- rbind(df1, df2)
print(df) Expected Output Copy Output
id name
1 1 Ann
2 2 Bob
3 3 Cara R Program to Change Column Name of a Dataframe Copy Code
Renames a column using names().
Program (R)
df <- data.frame(name=c("Ann","Bob"), age=c(19,21))
names(df)[2] <- "age_years"
print(names(df)) Expected Output Copy Output
[1] "name" "age_years" R Program to Drop Columns in a Dataframe Copy Code
Removes a column by assigning NULL.
Program (R)
df <- data.frame(name=c("Ann","Bob"), age=c(19,21))
df$age <- NULL
print(df) Expected Output Copy Output
name
1 Ann
2 Bob R Program to Reorder Columns in a Dataframe Copy Code
Changes column order by indexing.
Program (R)
df <- data.frame(name=c("Ann","Bob"), age=c(19,21))
print(df[, c("age", "name")]) Expected Output Copy Output
age name
1 19 Ann
2 21 Bob R Program to Split Dataframe Copy Code
Splits data frame by a grouping column.
Program (R)
df <- data.frame(group=c("A","A","B"), val=c(1,2,3))
spl <- split(df, df$group)
print(spl) Expected Output Copy Output
$A
group val
1 A 1
2 A 2
$B
group val
3 B 3 R Program to Merge Multiple Dataframes Copy Code
Merges by key using merge.
Program (R)
df1 <- data.frame(id=c(1,2), a=c(10,20))
df2 <- data.frame(id=c(2,3), b=c(200,300))
print(merge(df1, df2, by="id", all=TRUE)) Expected Output Copy Output
id a b
1 1 10 NA
2 2 20 200
3 3 NA 300 R Program to Delete Rows From Dataframe Copy Code
Filters rows using a condition.
Program (R)
df <- data.frame(name=c("Ann","Bob","Cara"), age=c(19,21,18))
df <- df[df$age > 19, ]
print(df) Expected Output Copy Output
name age
2 Bob 21 R Program to Make a List of Dataframes Copy Code
Creates a list containing multiple data frames.
Program (R)
df1 <- data.frame(id=1:2)
df2 <- data.frame(id=3:4)
lst <- list(df1=df1, df2=df2)
print(lapply(lst, nrow)) Expected Output Copy Output
$df1
[1] 2
$df2
[1] 2 R Program to Concatenate Two Strings Copy Code
Concatenates strings using paste0.
Program (R)
a <- "Hello"
b <- "World"
print(paste0(a, " ", b)) Expected Output Copy Output
[1] "Hello World" Frequently Asked QuestionsUse 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 examsResearchers — statistical analysis for papersAnalysts — data wrangling skills for workCareer switchers — transitioning into data rolesPractice Online: Online R Compiler & Editor (/tools/r-compiler-editor) · Related: All R Examples · Python Examples