replacec {lazy.tools} | R Documentation |
Replace Characters by New Ones
replacec(from, to, char, n = 1, start = 1, fixed = 1, ...)
from |
A character string to be replaced by "to". By default, there are no need to escape special characters. |
to |
A character string to replace "from". |
char |
A character vector where replacement occur. |
n |
# of times to replace |
start |
The location from which the replacement starts. |
fixed |
A parameter to be passed to gregexpr and gsub. |
... |
Additional parameters to gregexpr and gsub. |
Do not use regular expressions in from such as [a-b] etc.
When from does not contain regular expression,
fixed=0 and ignoe.case=1
should work.
A string with replacement.
char <- paste( paste(letters,collapse=""),"0123456789",'"'
, "'`!#$%&'()[]{}<>*+-,.;:/^~=_|", sep="", collapse="" )
lchar <- nchar(char)
char <- paste(char,char,char,sep=" ",collapse="")
# one to one
replacec( "b", "B", char )
replacec( "b", "B", char, 2 )
replacec( "b", "B", char, 1, 135)
# many to one
replacec( "bc", "B", char )
replacec( "bc", "B", char, 2 )
replacec( "bc", "B", char, 1, 135 )
# one to many
replacec( "b", "BC", char )
replacec( "b", "BC", char, 2 )
replacec( "b", "BC", char, 1, 135 )
# to null string
replacec( "b", "", char, 99 )
# special characters
replacec( "(", "[paren]", char )
replacec( "^", "[hat]", char )
replacec( "!#$", "[esd]", char )
# ignore.case
replacec("B","[B]",char, fixed=0, ignore.case=1 )