Home > Back-end >  saving lists as elements of matrix
saving lists as elements of matrix

Time:09-21

I am trying to add lists as an element of matrix. I am using the following code

wiip <- matrix(list(), 5, 5)
for (i in colnames(wave1)) {
    for (j in colnames(wave1)) {
        wiip[[i,j]] <- gclo(wave1[[i]], wave1[[j]])
    }
}

the wiip[[i,j]] says subscripts out of bounds. the output from the function gclo is a list and I want to save all the outputs. that is the double loop creates i*j(i runs over 1 to 5 and so does j, hence total 25 lists objects are created) number of lists. how to save each list together in one object.

CodePudding user response:

Try to save the result in a list and use [, i] and [,j] to subset each column of the matrix.

wiip <- vector('list', ncol(wave1)*ncol(wave1))
ind <- 0

for (i in colnames(wave1)) {
  for (j in colnames(wave1)) {
    ind <- ind   1
    wiip[[ind]] <- gclo(wave1[, i], wave1[, j])
  }
}
  • Related