# NOT RUN {
##
## 1. numerics
##
# 1.1. standard
xNum <- interpChar(1:3, 4:5, (0:3)/4)
# answer
xN. <- c(1, 2.75, 3.5, 4)
# }
# NOT RUN {
all.equal(xNum, xN.)
# }
# NOT RUN {
# 1.2. with x but not y:
# return that vector with a warning
# }
# NOT RUN {
xN1 <- Interp(1:4, p=.5)
# answer
xN1. <- 1:4
# }
# NOT RUN {
all.equal(xN1, xN1.)
# }
# NOT RUN {
##
## 2. Single character vector
##
# }
# NOT RUN {
i.5 <- Interp(c('a', 'bc', 'def'), character(0), p=0.3)
# with y = NULL or character(0),
# Interp returns x
# }
# NOT RUN {
all.equal(i.5, c('a', 'bc', 'def'))
# }
# NOT RUN {
i.5b <- Interp('', c('a', 'bc', 'def'), p=0.3)
# Cumulative characters (length(proportion)=1):
# 0.3*(total 6 characters) = 1.2 characters
i.5. <- c('a', 'b', '')
# }
# NOT RUN {
all.equal(i.5b, i.5.)
# }
# NOT RUN {
##
## 3. Reverse character example
##
i.5c <- Interp(c('a', 'bc', 'def'), '', 0.3)
# check: 0.7*(total 6 characers) = 4.2 characters
i.5c. <- c('a', 'bc', 'd')
# }
# NOT RUN {
all.equal(i.5c, i.5c.)
# }
# NOT RUN {
##
## 4. More complicated example
##
xCh <- Interp('', c('Do it', 'with R.'),
c(0, .5, .9))
# answer
xCh. <- c('', 'with', 'Do i')
# }
# NOT RUN {
all.equal(xCh, xCh.)
# }
# NOT RUN {
##
## 5. Still more complicated
##
xC2 <- Interp(c('a', 'fabulous', 'bug'),
c('bigger or', 'just', 'big'),
c(.3, .3, 1) )
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
# prop .3, .7, 1
# prop*d.char 2.4, 2.8, 0
# smaller+p*d 3, 7, 3
xC2. <- c('big', 'fabulou', 'big')
# }
# NOT RUN {
all.equal(xC2, xC2.)
# }
# NOT RUN {
##
## 6. with one NULL
##
null1 <- Interp(NULL, 1, .3)
# }
# NOT RUN {
all.equal(null1, 1)
# }
# NOT RUN {
null2 <- Interp('abc', NULL, .3)
# }
# NOT RUN {
all.equal(null2, 'abc')
# }
# NOT RUN {
##
## 7. length=0
##
log0 <- interpChar(logical(0), 2, .6)
# }
# NOT RUN {
all.equal(log0, 1.2)
# }
# NOT RUN {
##
## 8. Date
##
Jan1.1980 <- as.Date('1980-01-01')
Jan1.1972i <- Interp(0, Jan1.1980, .2)
# check
Jan1.1972 <- as.Date('1972-01-01')
# }
# NOT RUN {
all.equal(Jan1.1972, round(Jan1.1972i))
# }
# NOT RUN {
##
## 9. POSIXct
##
Jan1.1980c <- as.POSIXct(Jan1.1980)
Jan1.1972ci <- Interp(0, Jan1.1980c, .2)
# check
Jan1.1972ct <- as.POSIXct(Jan1.1972)
# }
# NOT RUN {
abs(difftime(Jan1.1972ct, Jan1.1972ci,
units="days"))<0.5
# }
Run the code above in your browser using DataLab