R Under development (unstable) (2024-09-10 r87115 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(abind) > options(error=function() NULL) > x <- array(0, dim=c(2,3,4),dimnames=list(letters[1:2],LETTERS[1:3],letters[23:26])) > # What we want to do here is get slices x[i,c("A","B"),c("w","x","y")] (for all i) > # to be matrix(1:6,ncol=3). > # If we assign in the standard way, and specify submatrices of the same shape in > # indices and as the value of the assign (but leave some indices on the LHS empty), > # then it is possible that submatrices do not end up being assigned to in the way > # we might expect, because the RHS value is flattened and replicated. > # This is only a problem when the repeating index has a lower dim number (here 1) > # than the specified indices (here 2 and 3). > x[,c("A","B"),c("w","x","y")] <- matrix(1:6,ncol=3) > x[1,c("A","B"),c("w","x","y")] w x y A 1 5 3 B 3 1 5 > x[2,c("A","B"),c("w","x","y")] w x y A 2 6 4 B 4 2 6 > # Assign in a way so that the RHS has its elements laid out in the same way > # as the LHS. > x[,c("A","B"),c("w","x","y")] <- rep(matrix(1:6,ncol=3), each=2) > # first slice > x[1,c("A","B"),c("w","x","y")] w x y A 1 3 5 B 2 4 6 > # second slice > x[2,c("A","B"),c("w","x","y")] w x y A 1 3 5 B 2 4 6 > > # now do it with afill() > x[] <- 0 > afill(x, TRUE, , ) <- matrix(1:6,ncol=3, dimnames=list(c("A","B"),c("w","x","y"))) > x[1,c("A","B"),c("w","x","y")] w x y A 1 3 5 B 2 4 6 > x[2,c("A","B"),c("w","x","y")] w x y A 1 3 5 B 2 4 6 > # mix up the order of the RHS of the assignment, afill will sort it back to match the LHS > x[] <- 0 > afill(x, T, , ) <- matrix(1:6,ncol=3, dimnames=list(c("A","B"),c("w","x","y")))[2:1,] > x[1,c("A","B"),c("w","x","y")] w x y A 1 3 5 B 2 4 6 > x[2,c("A","B"),c("w","x","y")] w x y A 1 3 5 B 2 4 6 > table(x==0) FALSE TRUE 12 12 > > # 4-d example > x <- array(0, dim=c(2,3,3,4),dimnames=list(letters[1:2],LETTERS[1:3],letters[24:26],LETTERS[23:26])) > x[1,c("A","B"),1,c("W","X","Y")] <- 1:6 > x[1,c("A","B"),2,c("W","X","Y")] <- 1:6 > x[1,c("A","B"),3,c("W","X","Y")] <- 1:6 > x[2,c("A","B"),1,c("W","X","Y")] <- 1:6 > x[2,c("A","B"),2,c("W","X","Y")] <- 1:6 > x[2,c("A","B"),3,c("W","X","Y")] <- 1:6 > c(x[1:2,c("A","B"),1:3,c("W","X","Y")]) [1] 1 1 2 2 1 1 2 2 1 1 2 2 3 3 4 4 3 3 4 4 3 3 4 4 5 5 6 6 5 5 6 6 5 5 6 6 > c(matrix(1:6, ncol=3)[rep(1:2, each=2),rep(1:3,each=3)]) [1] 1 1 2 2 1 1 2 2 1 1 2 2 3 3 4 4 3 3 4 4 3 3 4 4 5 5 6 6 5 5 6 6 5 5 6 6 > > afill(x, T, , T, ) <- matrix(1:6,ncol=3, dimnames=list(c("A","B"),c("W","X","Y"))) > x[1,c("A","B"),1,c("W","X","Y")] W X Y A 1 3 5 B 2 4 6 > x[1,c("A","B"),2,c("W","X","Y")] W X Y A 1 3 5 B 2 4 6 > x[1,c("A","B"),3,c("W","X","Y")] W X Y A 1 3 5 B 2 4 6 > x[2,c("A","B"),1,c("W","X","Y")] W X Y A 1 3 5 B 2 4 6 > x[2,c("A","B"),2,c("W","X","Y")] W X Y A 1 3 5 B 2 4 6 > x[2,c("A","B"),3,c("W","X","Y")] W X Y A 1 3 5 B 2 4 6 > table(x==0) FALSE TRUE 36 36 > > # 2-d example > x <- array(1:24, dim=c(6,4), dimnames=list(LETTERS[1:6], letters[23:26])) > x1 <- x > x1[2:4,2:3] <- -(1:6) > x1 w x y z A 1 7 13 19 B 2 -1 -4 20 C 3 -2 -5 21 D 4 -3 -6 22 E 5 11 17 23 F 6 12 18 24 > x1 <- x > x1[LETTERS[2:4],letters[24:25]] <- -(1:6) > x1 w x y z A 1 7 13 19 B 2 -1 -4 20 C 3 -2 -5 21 D 4 -3 -6 22 E 5 11 17 23 F 6 12 18 24 > x2 <- x > afill(x2) <- array(-(1:6),dim=c(3,2), dimnames=list(LETTERS[2:4],letters[24:25])) > x2 w x y z A 1 7 13 19 B 2 -1 -4 20 C 3 -2 -5 21 D 4 -3 -6 22 E 5 11 17 23 F 6 12 18 24 > identical(x1, x2) [1] TRUE > x2 <- x > afill(x2) <- array(-(1:6),dim=c(3,2), dimnames=list(LETTERS[5:7],letters[24:25])) Error in `afill<-.default`(`*tmp*`, value = -1:-6) : value has dimnames that are not in 'x' on dim[1]: 'G' Calls: afill<- -> afill<-.default > x2 <- x > afill(x2,excess.ok=T) <- array(-(1:6),dim=c(3,2), dimnames=list(LETTERS[5:7],letters[24:25])) > x2 w x y z A 1 7 13 19 B 2 8 14 20 C 3 9 15 21 D 4 10 16 22 E 5 -1 -4 23 F 6 -2 -5 24 > x2 <- x > afill(x2, local=T) <- array(-(1:6),dim=c(3,2), dimnames=list(LETTERS[2:4],letters[24:25])) > x2 w x y z A 1 7 13 19 B 2 -1 -4 20 C 3 -2 -5 21 D 4 -3 -6 22 E 5 11 17 23 F 6 12 18 24 > > # 1-d named-vector example > x <- c(A=0,B=0,C=0,D=0) > afill(x) <- c(B=1,C=2) > x A B C D 0 1 2 0 > # return value is the part of x that is assigned to > (afill(x) <- c(B=1,C=2)) B C 1 2 > (x[2:3] <- 0) [1] 0 > > proc.time() user system elapsed 0.12 0.06 0.17