I posted on Stackoverflow but this question has not gained much visibility. Original post here: https://stackoverflow.com/questions/71789201/rearrange-a-list-multiple-times-with-specific-patterns-and-save-all-the-outputs

I'd like to create 15 extra lists from an original list `L`

using a circulant matrix of patterns with the following custom function.

```
circ_list <- function(L, patterns){
M <- vector(mode = 'list', length = nrow(patterns))
if (patterns == patterns[1,]){
M[j,] <- L
}
else {
for (i in 2:nrow(patterns)) {
M[j,] <- L[patterns[i,]]
}
}
}
```

What I did was:

- Create an empty list
`M`

to hold all the outputs - Loop through the
`patterns`

row by row, at row 1 of`patterns`

, fill`M`

's nested list 1 with the original list`L`

. - Continue the loop, at row 2 of
`patterns`

, fill`M`

's nested list 2 with a shifted version of`L`

, as defined in the second row of`patterns`

, and so on.

I got an error

```
circ_list(lst1, patterns)
Warning in if (patterns == patterns[1, ]) { :
the condition has length > 1 and only the first element will be used
Error in M[j, ] <- L : object 'j' not found
```

To manually create each of the 15 extra lists

```
lst2 <- lst1[patterns[2,]]
lst3 <- lst1[patterns[3,]]
list(p4.b4 = structure(0:3, .Dim = c(2L, 2L)), p1.b1 = structure(1:4, .Dim = c(2L,
2L)), p1.b2 = structure(2:5, .Dim = c(2L, 2L)), p1.b3 = structure(12:15, .Dim = c(2L,
2L)), p1.b4 = structure(7:10, .Dim = c(2L, 2L)), p2.b1 = structure(10:13, .Dim = c(2L,
2L)), p2.b2 = structure(5:8, .Dim = c(2L, 2L)), p2.b3 = structure(6:9, .Dim = c(2L,
2L)), p2.b4 = structure(11:14, .Dim = c(2L, 2L)), p3.b1 = structure(10:13, .Dim = c(2L,
2L)), p3.b2 = structure(5:8, .Dim = c(2L, 2L)), p3.b3 = structure(3:6, .Dim = c(2L,
2L)), p3.b4 = structure(1:4, .Dim = c(2L, 2L)), p4.b1 = structure(2:5, .Dim = c(2L,
2L)), p4.b2 = structure(5:8, .Dim = c(2L, 2L)), p4.b3 = structure(4:7, .Dim = c(2L,
2L)))
> dput(lst3)
list(p4.b3 = structure(4:7, .Dim = c(2L, 2L)), p4.b4 = structure(0:3, .Dim = c(2L,
2L)), p1.b1 = structure(1:4, .Dim = c(2L, 2L)), p1.b2 = structure(2:5, .Dim = c(2L,
2L)), p1.b3 = structure(12:15, .Dim = c(2L, 2L)), p1.b4 = structure(7:10, .Dim = c(2L,
2L)), p2.b1 = structure(10:13, .Dim = c(2L, 2L)), p2.b2 = structure(5:8, .Dim = c(2L,
2L)), p2.b3 = structure(6:9, .Dim = c(2L, 2L)), p2.b4 = structure(11:14, .Dim = c(2L,
2L)), p3.b1 = structure(10:13, .Dim = c(2L, 2L)), p3.b2 = structure(5:8, .Dim = c(2L,
2L)), p3.b3 = structure(3:6, .Dim = c(2L, 2L)), p3.b4 = structure(1:4, .Dim = c(2L,
2L)), p4.b1 = structure(2:5, .Dim = c(2L, 2L)), p4.b2 = structure(5:8, .Dim = c(2L,
2L)))
```

and so on.

This is the original list

```
> dput(lst1)
list(p1.b1 = structure(1:4, .Dim = c(2L, 2L)), p1.b2 = structure(2:5, .Dim = c(2L,
2L)), p1.b3 = structure(12:15, .Dim = c(2L, 2L)), p1.b4 = structure(7:10, .Dim = c(2L,
2L)), p2.b1 = structure(10:13, .Dim = c(2L, 2L)), p2.b2 = structure(5:8, .Dim = c(2L,
2L)), p2.b3 = structure(6:9, .Dim = c(2L, 2L)), p2.b4 = structure(11:14, .Dim = c(2L,
2L)), p3.b1 = structure(10:13, .Dim = c(2L, 2L)), p3.b2 = structure(5:8, .Dim = c(2L,
2L)), p3.b3 = structure(3:6, .Dim = c(2L, 2L)), p3.b4 = structure(1:4, .Dim = c(2L,
2L)), p4.b1 = structure(2:5, .Dim = c(2L, 2L)), p4.b2 = structure(5:8, .Dim = c(2L,
2L)), p4.b3 = structure(4:7, .Dim = c(2L, 2L)), p4.b4 = structure(0:3, .Dim = c(2L,
2L)))
```

This is the reference

```
pattern_o <- names(lst1)
circ<-function(x) {
n<-length(x)
matrix(x[matrix(1:n,n+1,n+1,byrow=T)[c(1,n:2),1:n]],n,n)
}
patterns <- circ(pattern_o)
> head(patterns)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,] "p1.b1" "p1.b2" "p1.b3" "p1.b4" "p2.b1" "p2.b2" "p2.b3" "p2.b4" "p3.b1" "p3.b2" "p3.b3" "p3.b4" "p4.b1" "p4.b2" "p4.b3" "p4.b4"
[2,] "p4.b4" "p1.b1" "p1.b2" "p1.b3" "p1.b4" "p2.b1" "p2.b2" "p2.b3" "p2.b4" "p3.b1" "p3.b2" "p3.b3" "p3.b4" "p4.b1" "p4.b2" "p4.b3"
[3,] "p4.b3" "p4.b4" "p1.b1" "p1.b2" "p1.b3" "p1.b4" "p2.b1" "p2.b2" "p2.b3" "p2.b4" "p3.b1" "p3.b2" "p3.b3" "p3.b4" "p4.b1" "p4.b2"
[4,] "p4.b2" "p4.b3" "p4.b4" "p1.b1" "p1.b2" "p1.b3" "p1.b4" "p2.b1" "p2.b2" "p2.b3" "p2.b4" "p3.b1" "p3.b2" "p3.b3" "p3.b4" "p4.b1"
[5,] "p4.b1" "p4.b2" "p4.b3" "p4.b4" "p1.b1" "p1.b2" "p1.b3" "p1.b4" "p2.b1" "p2.b2" "p2.b3" "p2.b4" "p3.b1" "p3.b2" "p3.b3" "p3.b4"
[6,] "p3.b4" "p4.b1" "p4.b2" "p4.b3" "p4.b4" "p1.b1" "p1.b2" "p1.b3" "p1.b4" "p2.b1" "p2.b2" "p2.b3" "p2.b4" "p3.b1" "p3.b2" "p3.b3"
```

The `circ`

function was from https://stackoverflow.com/questions/15795318/efficient-way-to-create-a-circulant-matrix-in-r

Thanks a lot for any leads.