String Manipulation

String manipulations are conducted using built-in functions.

Built in string functions

Currently (v0.8.6), the following built-in string related functions are available.

print( str1, str2 ... ) // output strings to console
num_to_str( num ) // convert number to string.
str_strip( str ) // remove white spaces on both side of string.
str_lstrip( str ) // remove white spaces on left side.
str_rstrip( str ) // remove white spaces on right side.
str_concat( str1, str2 ... ) // concatenate strings.
str_repeat( str , num ) // repeat str num times.
str_subset( str, num, num )  // subset strings using index that starts from one. UTF8 compatible.
str_to_num( str ) // convert string to number

Example: Combine names after stripping white spaces on both sides

df = data.frame(
  first = c("  Abraham", "  Adam  ", "George", "  Charles", "  Galileo", "Marie  ", " Nikola"),
  last =  c("Lincoln  ", "Smith  ", "Washington  ", "Darwin", "Galilei", "  Curie", "Tesla ")
)
code = '
  highlighted_first = str_concat( "|" , first, "|" )
  highlighted_last = str_concat( "|" , last, "|" )
  fullname = str_concat( "|" , str_strip(first), " " , str_strip(last) , "|" )
'
library(datasailr)
sail(df, code)
##       first         last highlighted_first highlighted_last            fullname
## 1   Abraham    Lincoln         |  Abraham|      |Lincoln  |   |Abraham Lincoln|
## 2    Adam        Smith          |  Adam  |        |Smith  |        |Adam Smith|
## 3    George Washington            |George|   |Washington  | |George Washington|
## 4   Charles       Darwin       |  Charles|         |Darwin|    |Charles Darwin|
## 5   Galileo      Galilei       |  Galileo|        |Galilei|   |Galileo Galilei|
## 6   Marie          Curie         |Marie  |        |  Curie|       |Marie Curie|
## 7    Nikola       Tesla          | Nikola|         |Tesla |      |Nikola Tesla|