R – Strings

R Strings can be denoted using single quote or double quote.

Examples:

> x <- “This is a string”

> x

[1] “This is a string”

> x <- ‘This is a string’

> x

[1] “This is a string”

Irrespective of whether we use single quote or double quote, the string output is displayed in double quotes.

String Manipulation Functions:

  • abbreviate () – This function is used to tweak a string from either left or right side
  • agrep () – This function searches pattern like string within each element of the string.
  • as.character() – This function is used to convert non -character function to character function.
  • casefold() – This function is used for converting all characters in a string to lower/upper case. By default, it converts to lower case.
  • cat() – This function is used to concatenate objects.
  • char.expand() – This function output is a match of its first argument with the elements of its second
  • charmatch() – This function gives output of matches between 2 arguments.
  • chartr() – This function does string substitution
  • charToRaw() – This function converts character to ASCII or raw objects.
  • character() – This function is used to create character string.
  • dquote() – This function double quote the string
  • encodeString() – This function escapes the strings in a character vector
  • format() – This function assumes the elements of a vector as character string with a common format.
  • grep() – This function is used for finding a match
  • grepl() – This function is called logical grep and used for finding a match with logical output as TRUE/FALSE
  • gregexpr() – This function displays the output in the form of a list by searching a pattern in a string vector.
  • gsub() – This function searches for a pattern in a string vector and replace all occurrence of the pattern.
  • intersect() – This function is used to get common element from multiple vector
  • identical() – This is used to check if both the vectors are exactly equal to each other.
  • is.character() – This function is used to test if an object is of type “character”.
  • is.element() – This function is used to test if an element is contained in a given set of character string.
  • nchar () – This function counts the number of characters in each element of a character vector
  • nzchar() – This function checks whether elements of a character vector are non-empty strings.
  • noquote() – This function displays output strings without quote.
  • paste() – This function concatenate vectors after converting to character.
  • print() – This function print the output.
  • rep() – This function is used for repeating a string n times
  • replace() – This function replaces the values in a vector for the specified indices with a given value
  • regexpr() – This function tells the nth position of the each element in the string where a given string can be found
  • setdiff() – This function is used to get the difference of elements between 2 character vectors.
  • setequal() – This function is used to test the equality of 2 character vectors
  • sort() – This function is used to sort the element of a vector. Default is increasing order.
  • sprint() – This function prints output in a formatted string combining text and variable value.
  • squote() – This function prints the output in single quotes
  • sub() – This function is used for substitution of first matching element
  • stringr() – This function provides pattern matching functions to detect, extract, locate, match, replace and split strings.
  • str_c() – This function concatenates elements of a vectors with use of empty strings.
  • str_detect() – This function displays result in Boolean output . If a match is detected, it returns TRUE, if no match is detected it returns FALSE.
  • str_dup() – This function duplicates and concatenates within a character vector.
  • str_extract() – This function extracts the first matching pattern.
  • str_extract_all() – This function extracts all the matching patterns in a character vector
  • str_length() – This function returns the length of the string. For NA values its output is also NA
  • str_locate() – This function is used to locate the position of the first occurrence of a pattern in a string vector.
  • str_locate_all() – This function is used to locate the position of all occurrence of a pattern in a string vector
  • str_match() – This function extracts first match group of elements from the string vector.
  • str_match_all() –This function extract all matching group of elements from the string vector.
  • str_pad() – This function is used for padding a string
  • str_replace() –This function is used for replacing first occurrence of a matching pattern in a string.
  • str_replace_all() – This function is used for replacing all occurrence of a matching pattern in a string.
  • str_split() – This function is used for splitting the string
  • str_split_fixed() – This function is used for splitting the string in to a fixed number of pieces.
  • str_sub() – This function is used to extract sub strings from a character vector.
  • str_trim() – This function is used for trimming a string. Helpful in removing whitespaces at the end of a string
  • str_wrap() – This function is used to wrap a string in to format paragraphs.
  • strtoi() – This function converts strings in to integer
  • substr() – This function extracts substrings in a character vector.
  • substring() – This function replaces the substring in a character vector
  • tostring() – This function gives output with a single character string.
  • tolower() – This function converts string to lower case
  • toupper() – This function converts string to upper case
  • union() – This function makes union of elements between 2 character vector to produce a third vector