Learn R Programming

mvbutils (version 2.8.232)

multirep: Replacement and insertion functions with more/less than 1 replacement per spot

Description

multirep is like replace, but the replacements are a list of the same length as the number of elements to replace. Each element of the list can have 0, 1, or more elements-- the original vector will be expanded/contracted accordingly. (If all elements of the list have length 1, the result will be the same length as the original.) multinsert is similar, but doesn't overwrite the elements in orig (so the result of multinsert is longer). massrep is like multirep, but takes lists as arguments so that a group-of-line-numbers in the first list is replaced by a group-of-lines in the second list.

Usage

multirep( orig, at, repl, sorted.at=TRUE)
multinsert( orig, at, ins, sorted.at=TRUE)
massrep( orig, atlist, replist, sorted.at=TRUE)

Arguments

orig

vector

at

numeric vector, saying which elements of the original will be replaced or appended-to. Can't exceed length(orig). 0 is legal in multinsert but not multirep. Assumed sorted unless sorted.at is set to FALSE.

atlist

list where each element is a group of line numbers to be replaced by the corresponding element of replist (and that element can have a different length). Normally each group of line numbers would be consecutive, but this is not mandatory.

repl, ins, replist

a list of replacements. repl[[i]] will replace line at[i] in orig, possibly removing it (if repl[[i]] has length 0) or inserting extra elements (if repl[[i]] has length > 1). In multinsert, repl can be a non-list, whereupon it will be cast to list(repl) [if at is length 1] or as.list(repl) [if at is length>1]. If length(repl) < length(at), repl will be replicated to the appropriate size. If repl is atomic, it will be typecast into a list-- in this case, all replacements/insertions will be of length 1.

sorted.at

if TRUE, then at had better be sorted beforehand; if FALSE, at will be sorted for you inside multirep, and repl is reordered accordingly.

Examples

Run this code
# NOT RUN {
multirep( cq( the, cat, sat, on, the, mat), c( 2, 6),
    list( cq( big, bug), cq( elephant, howdah, cushion)))
# [1] "the" "big" "bug" "sat" "on" "the" "elephant" "howdah" "cushion"
multirep( cq( the, cat, sat, on, the, mat), c( 2, 6),
    list( cq( big, bug), character(0)))
# [1] "the" "big" "bug" "sat" "on" "the"
# NB the 0 in next example:
multinsert( cq( cat, sat, on, mat), c( 0, 4),
    list( cq( fat), cq( cleaning, equipment)))
# [1] "fat" "cat" "sat" "on" "mat" "cleaning" "equipment"
# }

Run the code above in your browser using DataLab