1
votes

I'm trying to figure out a way to write a loop (or apply-like function) that takes a particular row from a matrix within a list, writes it to a matrix/data frame, takes the matching row from the next list element, and places it after the previous one in the same row.

This involves a nested list which has a five matrices in the first part, five in the next part, and so on. Each chunk of matrices can vary in number of columns with a minimum of six and a maximum of eight. There are always four rows. The example below has 4x6 matrices in the first part and 4x8 matrices in the second part.

set.seed(100)
test.df <- data.frame(matrix(1:440,nrow=40,ncol=11))

mat1 <- matrix(rnorm(24),nrow=4,ncol=6)
mat2 <- matrix(rnorm(24),nrow=4,ncol=6)
mat3 <- matrix(rnorm(24),nrow=4,ncol=6)
mat4 <- matrix(rnorm(24),nrow=4,ncol=6)
mat5 <- matrix(rnorm(24),nrow=4,ncol=6)

mat6 <- matrix(rnorm(32),nrow=4,ncol=8)
mat7 <- matrix(rnorm(32),nrow=4,ncol=8)
mat8 <- matrix(rnorm(32),nrow=4,ncol=8)
mat9 <- matrix(rnorm(32),nrow=4,ncol=8)
mat10 <- matrix(rnorm(32),nrow=4,ncol=8)

test.list1 <- list(mat1,mat2,mat3,mat4,mat5)
test.list2 <- list(mat6,mat7,mat8,mat9,mat10)
list.f <- list(test.list1,test.list2)
res.mat <- matrix(nrow=2,ncol=40)

# (Edit) Example of expected results
res.mat[1,1:6] <- mat1[1,]
res.mat[1,9:14] <- mat2[1,]
res.mat[2,1:8] <- mat6[1,]
res.mat[2,9:16] <- mat7[1,]
res.mat
final.res <- cbind(test.df,res.mat)
final.res

The first row of each matrix in the first list of the nested list occupy the first row of res.mat. The first row of res.mat would have six entries, skip two columns (leaving the NAs) and then the next six entries and so on. Row two of res.mat would have eight entries, then the next eight and so on. Once res.mat is populated, I would append it to test.df as the final result. test.df is filled with a series of numbers as placeholders.

I have more than two higher level list elements, but for reproducibility, I've only included two. When it handles all of my data, there would be 2916 list elements and 2916 rows in the final data frame. Any ideas on how I could accomplish this would be greatly appreciated, and please ask if I can clarify anything with the code or description.

1
With this sample input, can you provide at least a couple of rows of expected output?r2evans
I've added some code to demonstrate the expected results. Does that help visualize things?Stephen

1 Answers

1
votes

In case anyone's interested, I did figure it out. Here's the solution:

format.dv <- function(list.f,test.df) {
nrow.f <- length(dvs)
  res.mat <- matrix(nrow=nrow.f,ncol=40)
  for (cond in 1:nrow(cond.list)) {
    ri <- 1
    min <- 1

    for (ri in 1:5) {

      data <- list.f[[cond]][[ri]]
      ncol <- dim(data)[2]
      adj <- 8 - ncol
      max <- 8
#Any number can be substituted for 1 depending on desired row to be taken
      pe.row <- data[1,]
      res.mat[cond,min:((max*ri)-adj)] <- pe.row
      min <- min+8
    }
  }
  res.mat <- round(res.mat,4)
  final.res <- cbind(test.df,res.mat)
  final.res
}