R Program to Concatenate a Vector of Strings
Concatenates using paste with collapse.
v <- c("a", "b", "c")
print(paste(v, collapse = ","))[1] "a,b,c"
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
Interactive Tool:Online R Compiler & Editor · Related:All R Examples · Python Examples · Java Examples · C Examples
Concatenates using paste with collapse.
v <- c("a", "b", "c")
print(paste(v, collapse = ","))[1] "a,b,c"
Prints a greeting using base R.
print("Hello, World!")[1] "Hello, World!"
Reads a value from console using readline and prints it.
name <- readline(prompt = "Enter your name: ")
cat("You entered:", name, "\n")Enter your name: Alice You entered: Alice
Concatenates strings using paste0.
a <- "Hello" b <- "World" print(paste0(a, " ", b))
[1] "Hello World"
Returns character count using nchar.
print(nchar("R language"))[1] 10
Checks presence using grepl.
s <- "Hello World"
print(grepl("World", s))[1] TRUE
Extracts a substring using substr.
s <- "Programiz" print(substr(s, 1, 7))
[1] "Program"
Replaces matching characters using gsub.
s <- "hello"
print(gsub("l", "x", s))[1] "hexxo"
Compares strings lexically ignoring case.
a <- "Apple"; b <- "apple" print(tolower(a) == tolower(b))
[1] TRUE
Converts a factor vector to character.
f <- factor(c("a", "b", "a"))
print(as.character(f))[1] "a" "b" "a"
Selects specific columns.
df <- data.frame(name=c("Ann","Bob"), age=c(19,21))
print(df[, c("name")])name 1 Ann 2 Bob
Trims spaces using trimws.
s <- " Hello World " print(trimws(s))
[1] "Hello World"
main.R, run Rscript main.R. Or use RStudio. R ranks among the top 20 languages on the TIOBE Index, dominant in academia and data science.
Practice Online:Online R Compiler & Editor (/tools/r-compiler-editor) · Related:All R Examples · Python Examples