| 1 |
"%nin%" <- function(x, table) !(x %in% table) |
| 2 |
|
| 3 |
reorder.array <- function(x,dim=1,names=NULL,indices=NULL,FUN=mean,...){ |
| 4 |
if(length(dim)>1) stop("'dim' has to be scalar") |
| 5 |
if(as.numeric(!missing(names)) + as.numeric(!missing(indices)) + as.numeric(!missing(FUN))>2) |
| 6 |
stop("too many arguments, use *either* 'names', 'indices', or 'FUN'") |
| 7 |
if(!missing(names)){ |
| 8 |
indices <- match(names,dimnames(x)[[dim]]) |
| 9 |
if(any(is.na(indices))) stop("unused dinnames in 'names' argument") |
| 10 |
other.indices <- setdiff(1:dim(x)[dim],indices) |
| 11 |
sort.indices <- c(indices,other.indices) |
| 12 |
} |
| 13 |
else if(!missing(indices)){ |
| 14 |
other.indices <- setdiff(1:dim(x)[dim],indices) |
| 15 |
sort.indices <- c(indices,other.indices) |
| 16 |
} else { |
| 17 |
results <- apply(x,dim,FUN) |
| 18 |
sort.indices <- order(results) |
| 19 |
} |
| 20 |
all.indices <- lapply(dim(x),function(d)1:d) |
| 21 |
all.indices[[dim]] <- sort.indices |
| 22 |
do.call("[",c(list(x),all.indices)) |
| 23 |
} |
| 24 |
|
| 25 |
reorder.matrix <- reorder.array |
| 26 |
|
| 27 |
lsl <- function() ls.str(envir=parent.frame()) |
| 28 |
|
| 29 |
split.matrix <- function (x, f, drop = FALSE, dim.drop=TRUE, ...){ |
| 30 |
index <- seq(nrow(x)) |
| 31 |
if(length(index)==length(f)){ |
| 32 |
index <- split.default(index,f,drop=drop) |
| 33 |
lapply(index,function(i)x[i,,drop=dim.drop]) |
| 34 |
} else { |
| 35 |
split(c(x),f,drop=drop,...) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
"split<-.matrix" <- function(x, f, drop = FALSE, ..., value) |
| 40 |
{ |
| 41 |
ix <- split(seq_along(x), f, drop = drop, ...) |
| 42 |
n <- length(value) |
| 43 |
j <- 0 |
| 44 |
for (i in ix) { |
| 45 |
j <- j%%n + 1 |
| 46 |
x[i, ] <- value[[j]] |
| 47 |
} |
| 48 |
x |
| 49 |
} |