Learn R Programming

fda (version 2.4.0)

checkDims3: Compare dimensions and dimnames of arrays

Description

Compare selected dimensions and dimnames of arrays, coercing objects to 3-dimensional arrays and either give an error or force matching.

Usage

checkDim3(x, y=NULL, xdim=1, ydim=1, defaultNames='x',
         subset=c('xiny', 'yinx', 'neither'),
         xName=substring(deparse(substitute(x)), 1, 33),
         yName=substring(deparse(substitute(y)), 1, 33) )
checkDims3(x, y=NULL, xdim=2:3, ydim=2:3, defaultNames='x',
         subset=c('xiny', 'yinx', 'neither'),
         xName=substring(deparse(substitute(x)), 1, 33),
         yName=substring(deparse(substitute(y)), 1, 33) )

Arguments

x, y
arrays to be compared. If y is missing, x is used. Currently, both x and y can have at most 3 dimensions. If either has more, an error will be thrown. If either has fewer, it will be
xdim, ydim
For checkDim3, these are positive integers indicating which dimension of x will be compared with which dimension of y. For checkDims3, these are positive integer vectors of the same
defaultNames
Either NULL, FALSE or a character string or vector or list. If NULL, no checking is done of dimnames. If FALSE, an error is thrown unless the corresponding dimensions of x and y match exactly. If it is a c
subset
If 'xiny', and any(dim(y)[ydim] < dim(x)[xdim]), an error is thrown. Else if any(dim(y)[ydim] > dim(x)[xdim]) the larger is reduced to match the smaller. If 'yinx', this procedure is reversed. If 'neither', any dimension mismatch g
xName, yName
names of the arguments x and y, used only to in error messages.

Value

  • a list with components x and y.

Details

For checkDims3, confirm that xdim and ydim have the same length, and call checkDim3 for each pair. For checkDim3, proceed as follows: 1. if((xdim>3) | (ydim>3)) throw an error. 2. ixperm <- list(1:3, c(2, 1, 3), c(3, 2, 1))[xdim]; iyperm <- list(1:3, c(2, 1, 3), c(3, 2, 1))[ydim]; 3. x3 <- aperm(as.array3(x), ixperm); y3 <- aperm(as.array3(y), iyperm) 4. xNames <- dimnames(x3); yNames <- dimnames(y3) 5. Check subset. For example, for subset='xiny', use the following: if(is.null(xNames)){ if(dim(x3)[1]>dim(y3)[1]) stop else y. <- y3[1:dim(x3)[1],,] dimnames(x) <- list(yNames[[1]], NULL, NULL) } else { if(is.null(xNames[[1]])){ if(dim(x3)[1]>dim(y3)[1]) stop else y. <- y3[1:dim(x3)[1],,] dimnames(x3)[[1]] <- yNames[[1]] } else { if(any(!is.element(xNames[[1]], yNames[[1]])))stop else y. <- y3[xNames[[1]],,] } } 6. return(list(x=aperm(x3, ixperm), y=aperm(y., iyperm)))

See Also

as.array3 plotfit.fd

Examples

Run this code
# Select the first two rows of y
stopifnot(all.equal(
checkDim3(1:2, 3:5),
list(x=array(1:2, c(2,1,1), list(c('x1','x2'), NULL, NULL)),
     y=array(3:4, c(2,1,1), list(c('x1','x2'), NULL, NULL)) )
))

# Select the first two rows of a matrix y
stopifnot(all.equal(
checkDim3(1:2, matrix(3:8, 3)),
list(x=array(1:2,         c(2,1,1), list(c('x1','x2'), NULL, NULL)),
     y=array(c(3:4, 6:7), c(2,2,1), list(c('x1','x2'), NULL, NULL)) )
))

# Select the first column of y
stopifnot(all.equal(
checkDim3(1:2, matrix(3:8, 3), 2, 2),
list(x=array(1:2,         c(2,1,1), list(NULL, 'x', NULL)),
     y=array(3:5, c(3,1,1), list(NULL, 'x', NULL)) )
))

# Select the first two rows and the first column of y
stopifnot(all.equal(
checkDims3(1:2, matrix(3:8, 3), 1:2, 1:2),
list(x=array(1:2, c(2,1,1), list(c('x1','x2'), 'x', NULL)),
     y=array(3:4, c(2,1,1), list(c('x1','x2'), 'x', NULL)) )
))

# Select the first 2 rows of y
x1 <- matrix(1:4, 2, dimnames=list(NULL, LETTERS[2:3]))
x1a <- x1. <- as.array3(x1)
dimnames(x1a)[[1]] <- c('x1', 'x2')
y1 <- matrix(11:19, 3, dimnames=list(NULL, LETTERS[1:3]))
y1a <- y1. <- as.array3(y1)
dimnames(y1a)[[1]] <- c('x1', 'x2', 'x3')

stopifnot(all.equal(
checkDim3(x1, y1),
list(x=x1a, y=y1a[1:2, , , drop=FALSE])
))

# Select columns 2 & 3 of y
stopifnot(all.equal(
checkDim3(x1, y1, 2, 2),
list(x=x1., y=y1.[, 2:3, , drop=FALSE ])
))

# Select the first 2 rows and  columns 2 & 3 of y
stopifnot(all.equal(
checkDims3(x1, y1, 1:2, 1:2),
list(x=x1a, y=y1a[1:2, 2:3, , drop=FALSE ])
))

# y = columns 2 and 3 of x
x23 <- matrix(1:6, 2, dimnames=list(letters[2:3], letters[1:3]))
x23. <- as.array3(x23)
stopifnot(all.equal(
checkDim3(x23, xdim=1, ydim=2),
list(x=x23., y=x23.[, 2:3,, drop=FALSE ])
))

# Transfer dimnames from y to x
x4a <- x4 <- matrix(1:4, 2)
y4 <- matrix(5:8, 2, dimnames=list(letters[1:2], letters[3:4]))
dimnames(x4a) <- dimnames(t(y4))
stopifnot(all.equal(
checkDims3(x4, y4, 1:2, 2:1),
list(x=as.array3(x4a), y=as.array3(y4))
))

# as used in plotfit.fd
daybasis65 <- create.fourier.basis(c(0, 365), 65)

daytempfd <- with(CanadianWeather, smooth.basis(
       day.5, dailyAv[,,"Temperature.C"], 
       daybasis65, fdnames=list("Day", "Station", "Deg C"))$fd )

defaultNms <- with(daytempfd, c(fdnames[2], fdnames[3], x='x'))
subset <- checkDims3(CanadianWeather$dailyAv[, , "Temperature.C"],
               daytempfd$coef, defaultNames=defaultNms)
# Problem:  dimnames(...)[[3]] = '1'
# Fix:
subset3 <- checkDims3(
        CanadianWeather$dailyAv[, , "Temperature.C", drop=FALSE],
               daytempfd$coef, defaultNames=defaultNms)

Run the code above in your browser using DataLab