swap.dim.list: Swap dimensions of a list

View source: R/swap_dim_list.R

swap.dim.listR Documentation

Swap dimensions of a list

Description

This function inverts the dimension of a given list; for instance, columns of each elements can become the new listing. All elements of the list must be matrices of same dimensions.

Usage

swap.dim.list(list, new.dim = c(1, 2, 3), progress.bar = TRUE)

Arguments

list

The list whose dimensions have to be swapped

new.dim

A numeric vector specifying the re-ordering. Hierarchy of elements is: rows, columns, elements (for i rows, j columns, and k elements, one has list[[k]][i,j])

progress.bar

A logical indicating whether a progression bar should be printed or not

Examples

# Create a simple list
list<-list("a"=matrix(1:15,ncol=3,nrow=5),"b"=matrix((1:15)^2,ncol=3,nrow=5),"c"=matrix((1:15)/2,ncol=3,nrow=5),"d"=matrix(log(1:15),ncol=3,nrow=5))
# Elements are named; set x1, x2.. and y1, y2... as row and column names for each matrix (so that we can ensure the swapping is done properly)
for(i in 1:length(list)){dimnames(list[[i]])<-list(paste("x",1:nrow(list[[i]]),sep=""),paste("y",1:ncol(list[[i]]),sep=""))}
list
# If wanting a list of 3 elements, each being a matrix of five columns and four rows (so that y are elements, x are columns, and a-b-c-d are rows)
swap.dim.list(list,new.dim=c(3,1,2)) # The new lines (first element of new.dim) are the previous elements, the new columns are the previous rows, and the new elements are the previous columns
# Same if wanting x to be elements, a-b-c-d to be columns, and y to be rows (so former columns become rows, former elements become columns, and former rows become elements)
swap.dim.list(list,new.dim=c(2,3,1))

jacobmaugoust/ULT documentation built on May 16, 2023, 1:29 p.m.