i

R Programming Complete Tutorial

R String Manipulation Functions

In this part of the document, we are going to cover the R string manipulation functions. We will discuss all the R string manipulation functions in this R tutorial along with their usage.

grep()

  • It is used for pattern matching and replacement
  • grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE, invert = FALSE)

nchar()

  • We can count the characters with the help of this function.  We can use a character vector as its argument, which then returns a vector comprising of different sizes of the elements of x.
  • nchar(x, type = "chars", allowNA = FALSE, keepNA = NA)

    paste()

  • It concatenates vectors after converting to character.
  • paste (..., sep = " ", collapse = NULL)

    format()

  • Format an R object for pretty printing.
  • format (x, trim = FALSE, digits = NULL, nsmall = 0L, justify = c("left", "right", "centre", "none"), width = NULL, na.encode = TRUE, scientific = NA, big.mark   = "",   big.interval = 3L,      small.mark = "", small.interval = 5L, decimal.mark = getOption("OutDec"), zero.print = NULL, drop0trailing = FALSE,…)

    sprintf()

  • A wrapper for the C function sprintf that returns a character vector containing a formatted combination of text and variable values.
  • sprintf(fmt, ...)

    substr()

  • Extract or replace substrings in a character vector.
  • substr(x, start, stop) <- value

    strsplit()

  • It splits the elements of a character vector x into substrings.
  • strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)

    toupper() &tolower()

  • It changes the case of the string.
  • tolower(x), toupper(x)