Description Usage Arguments Details Value Author(s) See Also Examples
For x
and y
logical, integer, numeric,
Date or POSIX:
xOut <- x*(1-.proportion) + y*.proportion
Otherwise, coerce to character and return a
substring
of x
or y
with
number of characters interpolating linearly between
nchar(x)
and nchar(y)
; see details.
*** NOTE: This function is currently in flux. The results may not match the documentation and may change in the future.
The current version does character interpolation on the cumulative number of characters with defaults with only one argument that may not be easy to understand and use. Proposed:
old: interpolate on number of characters in each string with the default for a missing argument being character(length(x)) [or character(length(y)) or numeric(length(x)) or ...]
2014-08-08: default with either x or y missing should be to set the other to the one we have, so interpChar becomes a no op – except that values with .proportion outside ("validProportion" = [0, 1] by default) should be dropped.
1 2 3 4 5 6 7 | interpChar(x, ...)
## S3 method for class 'list'
interpChar(x, .proportion,
argnames=character(3), message0=character(0), ...)
## Default S3 method:
interpChar(x, y, .proportion,
argnames=character(3), message0=character(0), ...)
|
x |
either a vector or a list. If a list, pass the first
two elements as the first two arguments of
|
y |
a vector |
.proportion |
A number or numeric vector assumed to be between 0 and 1. |
argnames |
a character vector of length 3 giving args |
message0 |
A character string to be passed with |
... |
optional arguments for |
1. x
, y
and .proportion
are first compared
for compatible lengths using compareLengths
. A
warning is issued if the lengths are not compatible. They are
then all extended to the same length using rep
.
2. If x
and y
are both numeric, interpChar
returns the standard linear interpolation (described above).
3. If x
, y
, and .proportion
are all
provided with at least one of x
and y
not being
numeric or logical, the algorithm does linear interpolation on
the difference in the number of characters between
x
and y
. It returns characters from
y
except when nchar(x)
> nchar(y)
,
in which case it returns characters from x
. This
meets the end conditions that the number of characters
matches that of x
when .proportion
is 0 and
matches that of y
when .proportion
is 1. This
can be used to "erase" characters moving from one frame to
the next in a video. See the examples.
4. If either x
or y
is missing, it is
replaced by a default vector of the same type and length;
for example, if y
is missing and x
is numeric,
y
= numeric(length(x))
. (If the one supplied
is not numeric or logical, it is coerced to character.)
A vector: Numeric if x
and y
are both numeric
and character otherwise. The length = max length of
x
, y
, and .proportion
.
Spencer Graves
interpPairs
, which calls interpChar
classIndex
, which is called by
interpChar
to help decide the class of the
interpoland.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | ##
## 1. numerics
##
# 1.1. standard
xNum <- interpChar(1:3, 4:5, (0:3)/4)
# answer
xN. <- c(1, 2.75, 3.5, 4)
all.equal(xNum, xN.)
# 1.2. list of length 1 with a numeric vector:
# return that vector with a warning
xN1 <- interpChar(list(a.0=1:4), .5)
# answer
xN1. <- 1:4
all.equal(xN1, xN1.)
##
## 2. Single character vector
##
i.5 <- interpChar(list(c('a', 'bc', 'def')), .p=0.3)
# If cumulative characters:
# 0.3*(total 6 characters) = 1.8 characters
#
# However, the current code does something different,
# returning "a", "bc", "d" <- like using 1-.p?
# This is a problem with the defaults with a single
# argument; ignore this issue for now.
# 2014-06-04
i.5. <- c('a', 'b', '')
#all.equal(i.5, i.5.)
##
## 3. Reverse character example
##
i.5c <- interpChar(c('a', 'bc', 'def'), '', 0.3)
# check: 0.7*(total 6 characers) = 4.2 characters
i.5c. <- c('a', 'bc', 'd')
all.equal(i.5c, i.5c.)
# The same thing specified in a list
i.5d <- interpChar(list(c('a', 'bc', 'def'), ''), 0.3)
all.equal(i.5d, i.5c.)
##
## 4. More complicated example
##
xCh <- interpChar(list(c('Do it', 'with R.')),
c(0, .5, .9))
# answer
xCh. <- c('', 'with', 'Do ')
# With only one input, it's assumed to be y.
# It is replicated to length(.proportion),
# With nchar = 5, 7, 5, cum = 5, 12, 17.
all.equal(xCh, xCh.)
##
## 5. Still more complicated
##
xC2 <- interpChar(c('a', 'fabulous', 'bug'),
c('bigger or', 'just', 'big'),
c(.3, .3, 1) )
# answer
x.y.longer <- c('bigger or', 'fabulous', 'big')
# use y with ties
# nch smaller 1 4 3
# nch larger 9 8 3
# d.char 8, 4, 0
# cum characters 8, 12, 12
# prop .3, .7, 1
# prop*12 3.6, 8.4, 12
# cum.sm 1, 5, 8
# cum.sm+prop*12 5, 13, 20
# -cum(larger[-1]) 5, 4, 3
xC2. <- c('bigge', 'fabu', 'big')
all.equal(xC2, xC2.)
##
## 6. with one NULL
##
null1 <- interpChar(NULL, 1, 1)
all.equal(null1, 1)
null2 <- interpChar('abc', NULL, .3)
all.equal(null2, 'ab')
##
## 7. length=0
##
log0 <- interpChar(logical(0), 2, .6)
all.equal(log0, 1.2)
##
## 8. Date
##
##
## 9. POSIXct
##
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.