##
## 1. A reasonable test with numerics, dates,
## an ordered factor and character variables
##
xDate <- as.Date('2001-02-03')+1:4
tstDF <- data.frame(x1=1:4, xDate=xDate,
xD2=as.POSIXct(xDate),
sex=ordered(c('M', 'F', 'M', 'F')),
huh=letters[c(1:3, 3)], stringsAsFactors=FALSE)
newDat <- Newdata(tstDF, 'xDate', n=5)
# check
newD <- data.frame(x1=2.5,
xDate=xDate[1]+seq(0, 3, length=5),
xD2=as.POSIXct(xDate[2]+0.5),
sex=ordered(c('M', 'F', 'M', 'F'))[2],
huh=letters[3], stringsAsFactors=FALSE)
attr(newD, 'out.attrs') <- attr(newDat, 'out.attrs')
stopifnot(
all.equal(newDat, newD)
)
##
## 2. Test with only one column
##
newDat1 <- Newdata(tstDF[, 2, drop=FALSE], 'xDate', n=5)
# check
newDat1. <- newD[, 2, drop=FALSE]
attr(newDat1., 'out.attrs') <- attr(newDat1, 'out.attrs')
stopifnot(
all.equal(newDat1, newDat1.)
)
##
## 3. Test with a factor
##
newSex <- Newdata(tstDF, 'sex')
# check
newS <- with(tstDF, data.frame(
x1=2.5, xDate=xDate[1]+1.5,
xD2=as.POSIXct(xDate[1]+1.5),
sex=ordered(c('M', 'F'))[2:1],
huh=letters[3], stringsAsFactors=FALSE) )
attr(newS, 'out.attrs') <- attr(newSex, 'out.attrs')
stopifnot(
all.equal(newSex, newS)
)
##
## 4. Test with an integer column number
##
newDat2 <- Newdata(tstDF, 2, n=5)
# check
stopifnot(
all.equal(newDat2, newD)
)
##
## 5. Test with all
##
NewAll <- Newdata(tstDF)
# check
tstLvls <- as.list(tstDF[c(1, 4), ])
tstLvls$sex <- tstDF$sex[2:1]
tstLvls$huh <- letters[c(3, 1)]
tstLvls$stringsAsFactors <- FALSE
NewA. <- do.call(expand.grid, tstLvls)
attr(NewA., 'out.attrs') <- attr(NewAll, 'out.attrs')
stopifnot(
all.equal(NewAll, NewA.)
)
Run the code above in your browser using DataLab