R Program to Replace Characters in a String
Replaces matching characters using gsub.
[1] "hexxo"
Work with strings—concatenate, measure length, search, extract, replace, compare, and convert factors. Practical text tasks for everyday R programming.
Replaces matching characters using gsub.
[1] "hexxo"
Prints a greeting using base R.
[1] "Hello, World!"
Reads a value from console using readline and prints it.
Enter your name: Alice You entered: Alice
Concatenates strings using paste0.
[1] "Hello World"
Returns character count using nchar.
[1] 10
Checks presence using grepl.
[1] TRUE
Extracts a substring using substr.
[1] "Program"
Compares strings lexically ignoring case.
[1] TRUE
Converts a factor vector to character.
[1] "a" "b" "a"
Selects specific columns.
name 1 Ann 2 Bob
Concatenates using paste with collapse.
[1] "a,b,c"
Trims spaces using trimws.
[1] "Hello World"
main.R, then run Rscript main.R from your terminal. Alternatively, use an IDE such as RStudio.
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.