Examples on text operations

Lower/upper, paste-ing

  • Uppercasing
  • Lowercasing
  • Paste-ing and collapsing text elements
from rlike import *
x = ['Some text', 'Another element']
x = toupper(x)
x = tolower(x)
paste(x, ["a", "b"], "END", sep = "-")
['some text-a-END', 'another element-b-END']
paste(x, ["a", "b"], "END", sep = "-", collapse = "\n")
'some text-a-END\nanother element-b-END'

Substr, splitting, trimming

  • Number of characters
  • Substring
  • Splitting strings
  • Trim whitespace
from rlike import *
x = ['Some text', 'Another element']
nchar(x)
[9, 15]
substr(x, start = 0, end = [3, 6])
['Some', 'Another']
strsplit(x, split = " +")
[['Some', 'text'], ['Another', 'element']]
trimws(['  ABC123\n ', '  ABC123\t\r '])
['ABC123', 'ABC123']

Regex

  • Pattern replacement
  • Look for a pattern
gsub(pattern = "[ \t\n\r]", replacement = "", x = ['  ABC123\n ', '  ABC123\t\r '])
['ABC123', 'ABC123']
grepl(pattern = r'\d+', x = ['abc.123.xyz', '5432 one liftoff 1.', 'Hello World'])
[True, True, False]