Skip to Main Content

Learn R

This guide focuses on transformation and cleaning functions in R that are especially useful for working with tabular datasets.

Basic String Manipulations

Concatenating Strings

String concatenation in R is done by the paste command.

Seperator (sep) is space by default.

Splitting Strings

Strings can be split in R using strsplit().

Substrings in R

Substrings in R can be retrieved from existing strings or replace some part of an original string. It can be achieved by substr. The general architecture of substr is:

substr(x, start, stop) where.

  • x – A character string.
  • start – If the characters of x were numbered, then the number of the first character to be returned (or overwritten).
  • stop – The number of the last character to be returned (or overwritten).

Match strings in R

Strings can be matched in R using str_detect(). Before using str_detect, you need to install and load stringr package.

To use str_detect function in R, make sure that you have the stringr package installed and loaded

> install.package(stringr)
> library(stringr)

Liaison Librarian

Profile Photo
Martin Morris
Contact:
Schulich Library of Physical Sciences, Life Sciences and Engineering
Macdonald-Stewart Library Building
809 rue Sherbrooke Ouest
Montréal, Québec H3A 0C1
(514) 398 8140
Website Skype Contact: martinatmcgill
Social: Twitter Page

McGill LibraryQuestions? Ask us!
Privacy notice