Title: | Tools for working with strings in R |
---|---|
Description: | Tools for working with strings in R |
Authors: | Sebastian Kranz |
Maintainer: | Sebastian Kranz <[email protected]> |
License: | GPL >= 2.0 |
Version: | 0.02 |
Built: | 2024-10-25 04:31:34 UTC |
Source: | https://github.com/skranz/stringtools |
Helper function
adapt.blocks.after.replace(block, ...)
adapt.blocks.after.replace(block, ...)
Helper function
adapt.pos.after.replace(pos, left, len.old, len.new)
adapt.pos.after.replace(pos, left, len.old, len.new)
Add level 0 to blocks
blocks.add.level.0(blocks, str, end = nchar(str))
blocks.add.level.0(blocks, str, end = nchar(str))
converts a matrix of of single chars in a vector of one string per row
char.matrix.to.str(mat, collapse = "")
char.matrix.to.str(mat, collapse = "")
converts a vector of chars into a single string or multiple strings, broken by sep
char.vector.to.str(vec, sep = NULL, collapse = "")
char.vector.to.str(vec, sep = NULL, collapse = "")
Check if parameter to a str function have allowed dimensions
check.str.par(str, para)
check.str.par(str, para)
Combine two positions via logical AND
combine.pos.and(pos1, pos2, return.ind = FALSE)
combine.pos.and(pos1, pos2, return.ind = FALSE)
Combine a list of positions via logical AND
combine.pos.list.and(pos.list, return.ind = TRUE)
combine.pos.list.and(pos.list, return.ind = TRUE)
Cummulative sum of number of characters that are ignored
## S3 method for class 'ignore' cumsum(ignore)
## S3 method for class 'ignore' cumsum(ignore)
## Not run: str =c("Now that is a nice matrix. Not?","but short!") ignore = rep(FALSE,max(nchar(str))) ignore[c(4:5,8:20)] = TRUE ignore cumsum.ignore(ignore) ignore = matrix(ignore,nrow=NROW(str),ncol=length(ignore),byrow=TRUE) cumsum.ignore(ignore) ## End(Not run)
## Not run: str =c("Now that is a nice matrix. Not?","but short!") ignore = rep(FALSE,max(nchar(str))) ignore[c(4:5,8:20)] = TRUE ignore cumsum.ignore(ignore) ignore = matrix(ignore,nrow=NROW(str),ncol=length(ignore),byrow=TRUE) cumsum.ignore(ignore) ## End(Not run)
Gets a logical ignore vector or list from pos matrices
get.ignore(ignore = NULL, ignore.pos = NULL, only.pos = NULL, end = NULL, str = NULL)
get.ignore(ignore = NULL, ignore.pos = NULL, only.pos = NULL, end = NULL, str = NULL)
Returns for every element of str whether there is a match with pattern works similar than grepl
has.substr(str, pattern, fixed = TRUE, perl = FALSE, ignore = NULL)
has.substr(str, pattern, fixed = TRUE, perl = FALSE, ignore = NULL)
## Not run: str = c("12347382709") pattern = c("a","4","56","34","766","b") has.substr(str,pattern) ## End(Not run)
## Not run: str = c("12347382709") pattern = c("a","4","56","34","766","b") has.substr(str,pattern) ## End(Not run)
Transforms a boolean vector ignore to a pos matrix and its complement
ignore.and.complement.pos(ignore = NULL, ignore.pos = NULL, only.pos = NULL)
ignore.and.complement.pos(ignore = NULL, ignore.pos = NULL, only.pos = NULL)
A list first element is the pos and complement matrix. The second element is a vector
Transforms a boolean vector ignore to a pos matrix
ignore.to.pos(ignore, complement = FALSE)
ignore.to.pos(ignore, complement = FALSE)
## Not run: ignore = c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,FALSE) ignore.to.pos(ignore) ignore = list(c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,FALSE),c(TRUE),c(),c(FALSE,FALSE)) ignore.to.pos(ignore) ignore = c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,FALSE) ignore.and.complement.pos(ignore) ## End(Not run)
## Not run: ignore = c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,FALSE) ignore.to.pos(ignore) ignore = list(c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,FALSE),c(TRUE),c(),c(FALSE,FALSE)) ignore.to.pos(ignore) ignore = c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,FALSE) ignore.and.complement.pos(ignore) ## End(Not run)
Combines c("A","B") into a single string seperated by line breaks
## S3 method for class 'lines' merge(txt, collapse = "\n")
## S3 method for class 'lines' merge(txt, collapse = "\n")
## Not run: merge = merge.lines(c("A","B")) merge sep.lines(merge) ## End(Not run)
## Not run: merge = merge.lines(c("A","B")) merge sep.lines(merge) ## End(Not run)
Returns the complement of a pos matrix again as a pos matrix
pos.complement(pos, is.sorted = FALSE, start = 1, end = NULL, keep.pos = FALSE, str = NULL)
pos.complement(pos, is.sorted = FALSE, start = 1, end = NULL, keep.pos = FALSE, str = NULL)
## Not run: pos = matrix(c( 2,2, 5,8, 10,10 ),3,2,byrow=TRUE) pos.complement(pos) pos.complement(pos,keep.pos=TRUE) ## End(Not run)
## Not run: pos = matrix(c( 2,2, 5,8, 10,10 ),3,2,byrow=TRUE) pos.complement(pos) pos.complement(pos,keep.pos=TRUE) ## End(Not run)
Transforms a pos matrix into a logical ignore vector Warning length is not
pos.to.ignore(pos, end = 1000000L, complement = FALSE, str = NULL)
pos.to.ignore(pos, end = 1000000L, complement = FALSE, str = NULL)
Returns a pos matrix combined with its complement. The matrix has an attribute "complement" which is a logical vector indicating whether a row in the matrix is the original matrix or a complement
pos.with.complement(pos, is.sorted = FALSE, end = NULL)
pos.with.complement(pos, is.sorted = FALSE, end = NULL)
## Not run: pos = matrix(c( 2,2, 5,8, 10,10 ),3,2,byrow=TRUE) pos.with.complement(pos) ## End(Not run)
## Not run: pos = matrix(c( 2,2, 5,8, 10,10 ),3,2,byrow=TRUE) pos.with.complement(pos) ## End(Not run)
strings will be treated as fixed constant in regex
regexp.fixed(str, fixed = TRUE)
regexp.fixed(str, fixed = TRUE)
str |
a string vector |
fixed |
if FALSE just return str |
transformed string vector of same size as str
## Not run: str = c("A.","*") # regexp.fixed transforms strings regexp.fixed(str) # fixed in stringr flags strings instead fixed(str) ## End(Not run)
## Not run: str = c("A.","*") # regexp.fixed transforms strings regexp.fixed(str) # fixed in stringr flags strings instead fixed(str) ## End(Not run)
replaces German Umlaute with ascii letters oe, ue, ae
replace.german.umlaute(txt = readLines(file), file = NULL, write.file = !is.null(file))
replace.german.umlaute(txt = readLines(file), file = NULL, write.file = !is.null(file))
transforms a single string with line breaks into a vector with one element for each line
sep.lines(txt, collapse = "\n")
sep.lines(txt, collapse = "\n")
pos is a matrix or a list of matrices specifying positions as returned by str.locate.all
## S3 method for class 'at.pos' str(str, pos)
## S3 method for class 'at.pos' str(str, pos)
## Not run: str = c("012ab0121","abce","0had112bb1") pos = str.locate.all(str,"[a-z]*",fixed=FALSE) pos str.at.pos(str,pos) return(ret) ## End(Not run)
## Not run: str = c("012ab0121","abce","0had112bb1") pos = str.locate.all(str,"[a-z]*",fixed=FALSE) pos str.at.pos(str,pos) return(ret) ## End(Not run)
Returns the between the first occurence of start and the first occurence of end
## S3 method for class 'between' str(str, start, end, ...)
## S3 method for class 'between' str(str, start, end, ...)
## Not run: str = c("a * (4+3)","b+3)+1","(a*3+(1+2))", ")") str.between(str,"(",")") str.between("#< type and","#< "," ") ## End(Not run)
## Not run: str = c("a * (4+3)","b+3)+1","(a*3+(1+2))", ")") str.between(str,"(",")") str.between("#< type and","#< "," ") ## End(Not run)
We allow for nested blocks. The position matrix also has an attribute level that describes the level of each block
## S3 method for class 'blocks.pos' str(str, start, end, ignore = NULL, ignore.start = ignore, ignore.end = ignore, fixed = TRUE, fixed.start = fixed, fixed.end = fixed)
## S3 method for class 'blocks.pos' str(str, start, end, ignore = NULL, ignore.start = ignore, ignore.end = ignore, fixed = TRUE, fixed.start = fixed, fixed.end = fixed)
## Not run: str = '1+(5*(2+3)+(2+(4-1)))' # 123456789012345678901 str.blocks.pos(str,"(",")") ## End(Not run)
## Not run: str = '1+(5*(2+3)+(2+(4-1)))' # 123456789012345678901 str.blocks.pos(str,"(",")") ## End(Not run)
Just a synonym for has.substr
## S3 method for class 'detect' str(str, pattern, fixed = TRUE, perl = FALSE, ignore = NULL)
## S3 method for class 'detect' str(str, pattern, fixed = TRUE, perl = FALSE, ignore = NULL)
Returns als elements of txt that end with pattern
Returns als elements of txt that end with pattern
## S3 method for class 'ends.with' str(txt, pattern) ## S3 method for class 'ends.with' str(txt, pattern)
## S3 method for class 'ends.with' str(txt, pattern) ## S3 method for class 'ends.with' str(txt, pattern)
## Not run: str = c("Hi how are you", "hi", "now what", "Hi") str.ends.with(str,"you") ## End(Not run)
## Not run: str = c("Hi how are you", "hi", "now what", "Hi") str.ends.with(str,"you") ## End(Not run)
Returns a list that contains for each element of str (or pattern) a vector of all substrings that match the pattern. If for a string no element is matched an empty list is returned
## S3 method for class 'extract.all' str(str, pattern, fixed = FALSE, perl = FALSE, ignore = NULL)
## S3 method for class 'extract.all' str(str, pattern, fixed = FALSE, perl = FALSE, ignore = NULL)
Returns a vector that contains for each element of str (or pattern) the first substring that matches pattern or NA if no match could be found
## S3 method for class 'extract.first' str(str, pattern, fixed = FALSE, perl = FALSE, ignore = NULL)
## S3 method for class 'extract.first' str(str, pattern, fixed = FALSE, perl = FALSE, ignore = NULL)
A general wrapper from str.locate.first, str.locate.all, str.extract.first, str.extract.all
## S3 method for class 'find' str(str, pattern, fixed = TRUE, first = FALSE, all = !first, simplify = TRUE, matches = FALSE, ...)
## S3 method for class 'find' str(str, pattern, fixed = TRUE, first = FALSE, all = !first, simplify = TRUE, matches = FALSE, ...)
str |
vector of strings that will be searched |
pattern |
a search pattern |
fixed |
if FALSE perform regular expression search |
first |
shall only the first element be returned |
all |
shall all elements be returned |
simplify |
try to simplify a list return |
matches |
if FALSE pos will returned, otherwise the extracted substrings |
Returns a logical vector with TRUE for every character of str that is in pos
## S3 method for class 'inpos' str(str, pos)
## S3 method for class 'inpos' str(str, pos)
keeps characters on left
## S3 method for class 'left' str(str, len = 1)
## S3 method for class 'left' str(str, len = 1)
Returns the substring left to the first occurence of pattern
## S3 method for class 'left.of' str(str, pattern, ..., not.found = str)
## S3 method for class 'left.of' str(str, pattern, ..., not.found = str)
## Not run: str = c("a = 5","b+3","a+3 = 4 = 3", "=") str.left.of(str,"=") str.left.of(str,"=", not.found=NA) str.right.of(str,"=") str.right.of(str,"=", not.found=NA) ## End(Not run)
## Not run: str = c("a = 5","b+3","a+3 = 4 = 3", "=") str.left.of(str,"=") str.left.of(str,"=", not.found=NA) str.right.of(str,"=") str.right.of(str,"=", not.found=NA) ## End(Not run)
a synonym for nchar
## S3 method for class 'len' str(str)
## S3 method for class 'len' str(str)
Transforms a vector of strings like c("A","B","C") into "A|B|C"
## S3 method for class 'list.to.regexp.or' str(str, fixed = TRUE)
## S3 method for class 'list.to.regexp.or' str(str, fixed = TRUE)
str |
a string vector |
fixed |
if TRUE treats str as constants in regexp |
a single string
## Not run: greek=c("alpha","beta","gamma") str.list.to.regexp.or(greek) ## End(Not run)
## Not run: greek=c("alpha","beta","gamma") str.list.to.regexp.or(greek) ## End(Not run)
Finds start and end positions of all substrings that match pattern
## S3 method for class 'locate.all' str(str, pattern, fixed = TRUE, perl = FALSE, ignore = NULL, ignore.pos = NULL, only.pos = NULL)
## S3 method for class 'locate.all' str(str, pattern, fixed = TRUE, perl = FALSE, ignore = NULL, ignore.pos = NULL, only.pos = NULL)
ignore.pos |
a logical vector or logical matrix indicating which locations of str shall be ignored in the search |
a list, of matrices n * 2 matrices. The first column is the start position, second column end position of each match
## Not run: str.locate.all("0120121","1") str.locate.all(c("0120121","abce","011"),"1") str = c("0120121","abce","011bb1") #str = c("0120121") ignore = rep(FALSE,max(nchar(str))) ignore[c(2:4)] = TRUE str.locate.all(str,"1",ignore=ignore) ignore.pos = rbind(c(2,4)) str.locate.all(str,"1",ignore.pos=ignore.pos) str.locate.all(str,c("1","b","a"),ignore=ignore) str = c("0120121") str.locate.all(str,c("1","b","2"),ignore=ignore) # Compare regular expression matching str = c("012ab0121","adch3b23","0123") gregexpr("[ab]*",str) str_locate_all(str,"[ab]*") str.locate.first(str,"[ab]*",fixed=FALSE) str.locate.all(str,"[ab]*",fixed=FALSE) str.locate.all(str,c("[ab]*","3+","0*"),fixed=FALSE) str.locate.first(str,c("[ab]*","2","0*"),fixed=FALSE) str.locate.all(str,"ab",fixed=FALSE) return(ret) ## End(Not run)
## Not run: str.locate.all("0120121","1") str.locate.all(c("0120121","abce","011"),"1") str = c("0120121","abce","011bb1") #str = c("0120121") ignore = rep(FALSE,max(nchar(str))) ignore[c(2:4)] = TRUE str.locate.all(str,"1",ignore=ignore) ignore.pos = rbind(c(2,4)) str.locate.all(str,"1",ignore.pos=ignore.pos) str.locate.all(str,c("1","b","a"),ignore=ignore) str = c("0120121") str.locate.all(str,c("1","b","2"),ignore=ignore) # Compare regular expression matching str = c("012ab0121","adch3b23","0123") gregexpr("[ab]*",str) str_locate_all(str,"[ab]*") str.locate.first(str,"[ab]*",fixed=FALSE) str.locate.all(str,"[ab]*",fixed=FALSE) str.locate.all(str,c("[ab]*","3+","0*"),fixed=FALSE) str.locate.first(str,c("[ab]*","2","0*"),fixed=FALSE) str.locate.all(str,"ab",fixed=FALSE) return(ret) ## End(Not run)
Locate a pattern at the end of str
## S3 method for class 'locate.at.end' str(str, pattern, fixed = TRUE)
## S3 method for class 'locate.at.end' str(str, pattern, fixed = TRUE)
## Not run: str.locate.at.end(c("0123456012","1230","012","01bsf"),"012") ## End(Not run)
## Not run: str.locate.at.end(c("0123456012","1230","012","01bsf"),"012") ## End(Not run)
Locate a pattern at the start of strings
## S3 method for class 'locate.at.start' str(str, pattern, fixed = TRUE)
## S3 method for class 'locate.at.start' str(str, pattern, fixed = TRUE)
## Not run: str.locate.at.start(c("0123456012","1230","012012","01bsf"),"012") str.locate.at.start("0123456",c("012","0","1")) str.locate.at.end(c("0123456012","1230","012","01bsf"),"012") ## End(Not run)
## Not run: str.locate.at.start(c("0123456012","1230","012012","01bsf"),"012") str.locate.at.start("0123456",c("012","0","1")) str.locate.at.end(c("0123456012","1230","012","01bsf"),"012") ## End(Not run)
Finds start and end positions of first substring that matches pattern
## S3 method for class 'locate.first' str(str, pattern, fixed = TRUE, perl = FALSE, ignore = NULL, ignore.pos = NULL, only.pos = NULL)
## S3 method for class 'locate.first' str(str, pattern, fixed = TRUE, perl = FALSE, ignore = NULL, ignore.pos = NULL, only.pos = NULL)
ignore.pos |
a logical vector or logical matrix indicating which locations of str shall be ignored in the search |
single.return is a 1*2 matrix. First column start position, second column end position
## Not run: str.locate.first("Hello","l") str.locate.first(c("Hello","What","lol"),"l") str.locate.first("Hello",c("l","e")) str.locate.first(c("Hello","What","lol"),c("l","g","o")) str = "Hello ernie!" ignore = rep(FALSE,max(nchar(str))) ignore[c(2:4)] = TRUE pos = str.locate.first(str,"e",ignore=ignore) pos str.split.at.pos(str,pos[,1],keep.pos=TRUE) ignore.pos = cbind(2,4) pos = str.locate.first(str,"e",ignore.pos=ignore.pos) pos str.split.at.pos(str,pos[,1],keep.pos=TRUE) str.detect(str,c("A","[a-z]*"),fixed=FALSE) str = c("Hello ernie","abcdefg","hello erna") pos = str.locate.first(str,"e",ignore=ignore) pos str.split.at.pos(str,pos,keep.pos=TRUE,pos.mat.like.list=TRUE) # Compare regular expression matching str = c("012ab0121","adch3b23","0123") regexpr("[ab]*",str) gregexpr("[ab]*",str) gregexpr("[ab]*",str,perl=TRUE) str_locate(str,c("b")) str_locate(str,"[ab]*") str_locate_all(str,"[ab]*") str.locate.first(str,"[ab]*",fixed=FALSE) str.detect(str,"[ab]*",fixed=FALSE) ## End(Not run)
## Not run: str.locate.first("Hello","l") str.locate.first(c("Hello","What","lol"),"l") str.locate.first("Hello",c("l","e")) str.locate.first(c("Hello","What","lol"),c("l","g","o")) str = "Hello ernie!" ignore = rep(FALSE,max(nchar(str))) ignore[c(2:4)] = TRUE pos = str.locate.first(str,"e",ignore=ignore) pos str.split.at.pos(str,pos[,1],keep.pos=TRUE) ignore.pos = cbind(2,4) pos = str.locate.first(str,"e",ignore.pos=ignore.pos) pos str.split.at.pos(str,pos[,1],keep.pos=TRUE) str.detect(str,c("A","[a-z]*"),fixed=FALSE) str = c("Hello ernie","abcdefg","hello erna") pos = str.locate.first(str,"e",ignore=ignore) pos str.split.at.pos(str,pos,keep.pos=TRUE,pos.mat.like.list=TRUE) # Compare regular expression matching str = c("012ab0121","adch3b23","0123") regexpr("[ab]*",str) gregexpr("[ab]*",str) gregexpr("[ab]*",str,perl=TRUE) str_locate(str,c("b")) str_locate(str,"[ab]*") str_locate_all(str,"[ab]*") str.locate.first(str,"[ab]*",fixed=FALSE) str.detect(str,"[ab]*",fixed=FALSE) ## End(Not run)
Check if str completely matches a pattern (not just a substring)
## S3 method for class 'matches.pattern' str(str, pattern, fixed = TRUE)
## S3 method for class 'matches.pattern' str(str, pattern, fixed = TRUE)
Returns the number of matches of pattern in each element of str
## S3 method for class 'number.matches' str(str, pattern, ...)
## S3 method for class 'number.matches' str(str, pattern, ...)
remove charcaters on left and right of a string str.remove.ends(c("ABCDEF","01"),1,3) returns c("BC","")
## S3 method for class 'remove.ends' str(str, left = 0, right = 0)
## S3 method for class 'remove.ends' str(str, left = 0, right = 0)
## Not run: str.remove.ends(c("ABCDEF","01345"),1,3) str.remove.ends(c("ABCDEF"),1:2,1:2) str.remove.ends(c("ABCDEF","01345"),1:2,1) # The following calls throw errors! str.remove.ends(c("ABCDEF","01345"),1:2,1:3) str.remove.ends(c("ABCDEF","01345","NOW!"),1:2,1) ## End(Not run)
## Not run: str.remove.ends(c("ABCDEF","01345"),1,3) str.remove.ends(c("ABCDEF"),1:2,1:2) str.remove.ends(c("ABCDEF","01345"),1:2,1) # The following calls throw errors! str.remove.ends(c("ABCDEF","01345"),1:2,1:3) str.remove.ends(c("ABCDEF","01345","NOW!"),1:2,1) ## End(Not run)
ignore is a logical vector or matrix stating which char positions shall be ignored the function removes the substrings for which ignore=TRUE
## S3 method for class 'remove.ignore' str(str, ignore)
## S3 method for class 'remove.ignore' str(str, ignore)
## Not run: str =c("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ","1234567890") ignore = rep(FALSE,max(nchar(str))) ignore[c(4:5,8:20)] = TRUE str str.remove.ignore(str,ignore) ## End(Not run)
## Not run: str =c("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ","1234567890") ignore = rep(FALSE,max(nchar(str))) ignore[c(4:5,8:20)] = TRUE str str.remove.ignore(str,ignore) ## End(Not run)
Replaces in str every occurence of pattern by replacement
## S3 method for class 'replace' str(str, pattern, replacement, fixed = TRUE, perl = FALSE, ignore = NULL, ignore.pos = NULL, only.pos = NULL, ignore.pattern = "_IGNORE_", ...)
## S3 method for class 'replace' str(str, pattern, replacement, fixed = TRUE, perl = FALSE, ignore = NULL, ignore.pos = NULL, only.pos = NULL, ignore.pattern = "_IGNORE_", ...)
str |
the string to replaced |
pattern |
the substring to be replaced |
replacment |
the new substrings |
a string
## Not run: str = c("12345678901234567890") pattern = c("34","12") replacement = c("AB","Holla die Waldfee") pos = cbind(1,10) str.replace(str,pattern,replacement, ignore.pos=pos) str.replace(str,pattern,replacement, only.pos=pos) str.replace(str,pattern,replacement) str = "int{5*2}*{2*3}" pattern = "int{_IGNORE_}" replacement = "integer{_IGNORE_}" pos = cbind(c(5,11),c(7,13)) str.replace(str,pattern,replacement, ignore.pos=pos) ## End(Not run)
## Not run: str = c("12345678901234567890") pattern = c("34","12") replacement = c("AB","Holla die Waldfee") pos = cbind(1,10) str.replace(str,pattern,replacement, ignore.pos=pos) str.replace(str,pattern,replacement, only.pos=pos) str.replace(str,pattern,replacement) str = "int{5*2}*{2*3}" pattern = "int{_IGNORE_}" replacement = "integer{_IGNORE_}" pos = cbind(c(5,11),c(7,13)) str.replace(str,pattern,replacement, ignore.pos=pos) ## End(Not run)
replace a string at the positions specified by pos
## S3 method for class 'replace.at.pos' str(str, pos, new, pos.mat.like.list = FALSE)
## S3 method for class 'replace.at.pos' str(str, pos, new, pos.mat.like.list = FALSE)
str |
a vector, or a single element |
pos |
a matrix of substring positions, or a list of such matrices if str is a vector |
new |
a vector of new strings for each substring position, or a list of such vectors if length(str)>1 |
string (vector) of length(str) in which the substrings have been replaced
## Not run: str = "1234567890" pos = rbind(c(7,7),c(4,5)) new = c("XXX","...") str.replace.at.pos(str,pos,new) str = c("1234567890","ahgdasdajsdgadhsabd") str.replace.at.pos(str,pos,new) ## End(Not run)
## Not run: str = "1234567890" pos = rbind(c(7,7),c(4,5)) new = c("XXX","...") str.replace.at.pos(str,pos,new) str = c("1234567890","ahgdasdajsdgadhsabd") str.replace.at.pos(str,pos,new) ## End(Not run)
Replaces in str every occurence of pattern by replacement
## S3 method for class 'replace.by.blocks' str(str, pattern, replacement, blocks = NULL, sub.txt = "SUB", block.start, block.end, block.ignore = NULL, use.levels = NULL, fixed = TRUE, only.replace.smaller.than = NULL, only.replace.larger.than = NULL)
## S3 method for class 'replace.by.blocks' str(str, pattern, replacement, blocks = NULL, sub.txt = "SUB", block.start, block.end, block.ignore = NULL, use.levels = NULL, fixed = TRUE, only.replace.smaller.than = NULL, only.replace.larger.than = NULL)
str |
the string to replaced |
pattern |
the substring to be replaced |
replacment |
the new substrings |
block |
a block retrieved from str.block.pos alternatively, you can provide block.start and block.end |
block.start |
string with which the blocks start, e.g. "(" |
block.end |
string with which the blocks end, e.g. ")" |
only.replace.smaller.than |
if not NULL only replaces matches whose number of characters is less or equal to only.replace.smaller.than |
only.replace.larger.than |
if not NULL only replaces matches whose number of characters is bigger or equal to only.replace.larger.than |
a string
## Not run: # Replace latex fractions str = "5+\frac{x^2+x^2}{1+\frac{2}{x*5}}*2" str.replace.by.blocks(str,"\frac{_SUB_}{_SUB_}","(_SUB1_)/(_SUB2_)", block.start = "{", block.end = "}") str.replace.by.blocks(str,"\frac{_SUB_}{_SUB_}","(_SUB1_)/(_SUB2_)", block.start = "{", block.end = "}", only.replace.larger.than=20) str.replace.by.blocks(str,"\frac{_SUB_}{_SUB_}","(_SUB1_)/(_SUB2_)", block.start = "{", block.end = "}", only.replace.smaller.than=20) str ="-\frac{\sigma_{m}-\beta\sigma_{b}}{\beta-1}=\frac{\sigma_{m}-\beta\sigma_{b}}{1-\beta}" str ="\frac{1}{2}=\frac{3}{4}" str.replace.by.blocks(str,"\frac{_SUB_}{_SUB_}","(_SUB1_)/(_SUB2_)", block.start = "{", block.end = "}") ## End(Not run)
## Not run: # Replace latex fractions str = "5+\frac{x^2+x^2}{1+\frac{2}{x*5}}*2" str.replace.by.blocks(str,"\frac{_SUB_}{_SUB_}","(_SUB1_)/(_SUB2_)", block.start = "{", block.end = "}") str.replace.by.blocks(str,"\frac{_SUB_}{_SUB_}","(_SUB1_)/(_SUB2_)", block.start = "{", block.end = "}", only.replace.larger.than=20) str.replace.by.blocks(str,"\frac{_SUB_}{_SUB_}","(_SUB1_)/(_SUB2_)", block.start = "{", block.end = "}", only.replace.smaller.than=20) str ="-\frac{\sigma_{m}-\beta\sigma_{b}}{\beta-1}=\frac{\sigma_{m}-\beta\sigma_{b}}{1-\beta}" str ="\frac{1}{2}=\frac{3}{4}" str.replace.by.blocks(str,"\frac{_SUB_}{_SUB_}","(_SUB1_)/(_SUB2_)", block.start = "{", block.end = "}") ## End(Not run)
A very slow implementation
## S3 method for class 'replace.list' str(str, pattern, replacement, ...)
## S3 method for class 'replace.list' str(str, pattern, replacement, ...)
## Not run: str.replace.list("na dies ist doch",c("a","e"),c("A","E")) ## End(Not run)
## Not run: str.replace.list("na dies ist doch",c("a","e"),c("A","E")) ## End(Not run)
keeps characters on right
## S3 method for class 'right' str(str, len = 1)
## S3 method for class 'right' str(str, len = 1)
Returns the substring right to the first occurence of pattern
## S3 method for class 'right.of' str(str, pattern, ..., not.found = str)
## S3 method for class 'right.of' str(str, pattern, ..., not.found = str)
Returns a string constisting of times spaces, vectorized over times
Returns a string constisting of times spaces, vectorized over times
## S3 method for class 'space' str(times, space = " ") ## S3 method for class 'space' str(times, space = " ")
## S3 method for class 'space' str(times, space = " ") ## S3 method for class 'space' str(times, space = " ")
Splits string vectors
## S3 method for class 'split' str(str, pattern, first = FALSE, keep.match = FALSE, ...)
## S3 method for class 'split' str(str, pattern, first = FALSE, keep.match = FALSE, ...)
str |
a vector of strings |
pattern |
vector where splits take place |
A list with same length as str. Each list element i contains the split substrings from str[i]
## Not run: str = "Hi\n\nyou!" str.split(str,"\n", keep.match=!TRUE) str <- c("aes_afe_f", "qwe.rty", "yui0op[3", "b") #split x on the letter e str str.split(str, "e", keep.match=TRUE) str.split(str, "e", first=TRUE, keep.match=TRUE) str = c("aes_afe_fe") ignore.pos = cbind(1,3) str.split(str, "e", keep.match=TRUE, ignore.pos=ignore.pos) str.split(str, "e", first=TRUE,keep.match=TRUE, ignore.pos=ignore.pos) str = "abscde3823nsd34" str.split(str, "[a-z]*", fixed=FALSE, keep.match=TRUE) str.split(str, c("[a-z]*","d"), fixed=FALSE, keep.match=TRUE) str = c("abscde3823nsd34","8748274") str.split(str, c("[a-z]*","d"), fixed=FALSE, keep.match=TRUE) ## End(Not run)
## Not run: str = "Hi\n\nyou!" str.split(str,"\n", keep.match=!TRUE) str <- c("aes_afe_f", "qwe.rty", "yui0op[3", "b") #split x on the letter e str str.split(str, "e", keep.match=TRUE) str.split(str, "e", first=TRUE, keep.match=TRUE) str = c("aes_afe_fe") ignore.pos = cbind(1,3) str.split(str, "e", keep.match=TRUE, ignore.pos=ignore.pos) str.split(str, "e", first=TRUE,keep.match=TRUE, ignore.pos=ignore.pos) str = "abscde3823nsd34" str.split(str, "[a-z]*", fixed=FALSE, keep.match=TRUE) str.split(str, c("[a-z]*","d"), fixed=FALSE, keep.match=TRUE) str = c("abscde3823nsd34","8748274") str.split(str, c("[a-z]*","d"), fixed=FALSE, keep.match=TRUE) ## End(Not run)
Splits a single string str at positions specified by pos
## S3 method for class 'split.at.pos' str(str, pos, keep.pos = FALSE, compl = NULL, max.char = max(nchar(str)), pos.mat.like.list = FALSE)
## S3 method for class 'split.at.pos' str(str, pos, keep.pos = FALSE, compl = NULL, max.char = max(nchar(str)), pos.mat.like.list = FALSE)
str |
character vector that shall be splitted |
pos |
split positions can be vector: assuming an element of size 1 that specifies a single char at that positions n*2 matrix: first column left position, right column right position list of vectors or matrices, specifying different pos for different str |
keep.pos |
default=FALSE. If TRUE add the tokens that describe the split to the result otherwise remove them |
single return is length of pos (if vector) or NCOL(pos) if pos is matrix
## Not run: str = c("1234567890") pos = c(3,5,7) str.split.at.pos(str,pos,keep.pos = FALSE) str.split.at.pos(str,pos,keep.pos = TRUE) pos = rbind(c(2,3),c(5,5),c(7,9)) str.split.at.pos(str,pos,keep.pos = FALSE) str.split.at.pos(str,pos,keep.pos = TRUE) # Multiple str str = c("Hello ernie","abcg","hello erna") pos = c(2,5,8) str.split.at.pos(str,pos,keep.pos=TRUE) pos = list(c(3,5),c(2),c(1,9)) str.split.at.pos(str,pos,keep.pos=TRUE) str = c("Hello ernie","abcdefg","hello erna") pos = str.locate.first(str,"e",ignore=ignore) pos str.split.at.pos(str,pos,keep.pos=TRUE,pos.mat.like.list=FALSE) str.split.at.pos(str,pos,keep.pos=TRUE,pos.mat.like.list=TRUE) ## End(Not run)
## Not run: str = c("1234567890") pos = c(3,5,7) str.split.at.pos(str,pos,keep.pos = FALSE) str.split.at.pos(str,pos,keep.pos = TRUE) pos = rbind(c(2,3),c(5,5),c(7,9)) str.split.at.pos(str,pos,keep.pos = FALSE) str.split.at.pos(str,pos,keep.pos = TRUE) # Multiple str str = c("Hello ernie","abcg","hello erna") pos = c(2,5,8) str.split.at.pos(str,pos,keep.pos=TRUE) pos = list(c(3,5),c(2),c(1,9)) str.split.at.pos(str,pos,keep.pos=TRUE) str = c("Hello ernie","abcdefg","hello erna") pos = str.locate.first(str,"e",ignore=ignore) pos str.split.at.pos(str,pos,keep.pos=TRUE,pos.mat.like.list=FALSE) str.split.at.pos(str,pos,keep.pos=TRUE,pos.mat.like.list=TRUE) ## End(Not run)
Returns als elements of txt that begin with pattern
## S3 method for class 'starts.with' str(txt, pattern)
## S3 method for class 'starts.with' str(txt, pattern)
## Not run: str = c("Hi how are you", "hi", "now what", "Hi") str.starts.with(str,"Hi") ## End(Not run)
## Not run: str = c("Hi how are you", "hi", "now what", "Hi") str.starts.with(str,"Hi") ## End(Not run)
An alternative interface to str.split
## S3 method for class 'tokenize' str(str, split = " ", only.one.split = FALSE, simplify = TRUE, ...)
## S3 method for class 'tokenize' str(str, split = " ", only.one.split = FALSE, simplify = TRUE, ...)
trims whitespaces from string
## S3 method for class 'trim' str(txt)
## S3 method for class 'trim' str(txt)
converts into a vector of strings into a matrix of single characters
to.char.matrix(str, drop = FALSE)
to.char.matrix(str, drop = FALSE)
## Not run: str =c("Now that is a nice matrix","but short!") mat = to.char.matrix(str) mat char.matrix.to.str(mat) vec = to.char.vector(str,collapse="\n") vec char.vector.to.str(vec,collapse="\n") ## End(Not run)
## Not run: str =c("Now that is a nice matrix","but short!") mat = to.char.matrix(str) mat char.matrix.to.str(mat) vec = to.char.vector(str,collapse="\n") vec char.vector.to.str(vec,collapse="\n") ## End(Not run)
converts a string into a vector of single characters
to.char.vector(str, collapse = "")
to.char.vector(str, collapse = "")