Working with Text in R

Interactive R tutorial on base R string tools: nchar, toupper, paste, substr, sub, grepl and strsplit β€” count, join, cut, replace, find and split text in your browser.

Base R has a full text toolkit β€” no packages needed. Edit and Run each box.

Watch: text tools in 90 seconds

Every step below β€” counting, joining, cutting, finding and splitting text β€” run live in RStudio, then try each one in the consoles that follow.

Download the R script  Β·  β–Ά Practice in the playground β€” or edit and run each snippet below.

Count characters, change case

Join text with paste

paste() takes a separator; paste0() glues with nothing β€” handy for IDs:

Cut a piece, replace a match

Find patterns with grepl

grepl() returns TRUE/FALSE β€” perfect for filtering:

Split a string apart

Your turn


You’ve completed the R Foundations path! Where next? Keep experimenting in the R playground, or move on to Biostatistics in R β€” Table 1, regression, survival and more, with the exact code.