replace-value

Finds and replaces one or more occurrences of a value in one or more columns of a datatable.

Finds and replaces one or more occurrences of a value in specified columns of a datatable. The pattern can be a text or a regular expression. If pattern is a text, only the first occurrence will be replaced. If pattern is a regular expression, all occurrences will be replaced.

Arguments

Inputs:

data: A datatable to be searched and modified.

pattern: A text or a regular expression to be replaced. Regular expressions must be wrapped with / character (e.g. /.*/

ignore case: Specifies whether lowercase and uppercase letters should be treated as equivalent. If unspecified, defaults to "True" (lowercase and uppercase are the same).

replacement: The text that replaces the substring matched by the specified pattern. If unspecified, matches will be replaced with a blank text (i.e. \\")

columns: A list of columns in data to be searched. If unspecified, it defaults to all columns in data.

Output:

data: A modified datatable. The column names are the same as the input, and all columns remain in the datatable. The values within the columns are modified as specified.

Possible use cases

  • Changing all occurrences of a sample ID in a datatable.

  • Fixing a common spelling error throughout a datatable.

  • Removing an unnecessary special character from a column's values.

  • Replacing spaces with underscores.

  • Removing mid-value commas to facilitate proper parsing of a CSV output.

Last updated