String Handling and Text Processing in R

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 Extract Columns From a Dataframe

Selects specific columns.

Program (R)
df <- data.frame(name=c("Ann","Bob"), age=c(19,21))
print(df[, c("name")])
Expected Output
  name
1  Ann
2  Bob

R "Hello World" Program

Prints a greeting using base R.

Program (R)
print("Hello, World!")
Expected Output
[1] "Hello, World!"

R Program to Take Input From User

Reads a value from console using readline and prints it.

Program (R)
name <- readline(prompt = "Enter your name: ")
cat("You entered:", name, "\n")
Expected Output
Enter your name: Alice
You entered: Alice

R Program to Concatenate Two Strings

Concatenates strings using paste0.

Program (R)
a <- "Hello"
b <- "World"
print(paste0(a, " ", b))
Expected Output
[1] "Hello World"

R Program to Find the Length of a String

Returns character count using nchar.

Program (R)
print(nchar("R language"))
Expected Output
[1] 10

R Program to Check if Characters are Present in a String

Checks presence using grepl.

Program (R)
s <- "Hello World"
print(grepl("World", s))
Expected Output
[1] TRUE

R Program to Extract n Characters From a String

Extracts a substring using substr.

Program (R)
s <- "Programiz"
print(substr(s, 1, 7))
Expected Output
[1] "Program"

R Program to Replace Characters in a String

Replaces matching characters using gsub.

Program (R)
s <- "hello"
print(gsub("l", "x", s))
Expected Output
[1] "hexxo"

R Program to Compare two Strings

Compares strings lexically ignoring case.

Program (R)
a <- "Apple"; b <- "apple"
print(tolower(a) == tolower(b))
Expected Output
[1] TRUE

R Program to Convert Factors to Characters

Converts a factor vector to character.

Program (R)
f <- factor(c("a", "b", "a"))
print(as.character(f))
Expected Output
[1] "a" "b" "a"

R Program to Concatenate a Vector of Strings

Concatenates using paste with collapse.

Program (R)
v <- c("a", "b", "c")
print(paste(v, collapse = ","))
Expected Output
[1] "a,b,c"

R Program to Trim Leading and Trailing Whitespaces

Trims spaces using trimws.

Program (R)
s <- "   Hello World   "
print(trimws(s))
Expected Output
[1] "Hello World"

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