I have a list X of three elements. Each element X[[i]] is a list of two – X1 and X2.
I want to construct a list X_new which would be a list of two elements X1 and X2, each element X_new[[i]] is a list of three.
This works perfectly fine:
X_new <- vector(mode = "list", length = 2)
X_new[[1]] <- lapply(X, function(x) x$X1)
X_new[[2]] <- lapply(X, function(x) x$X2)
But what if instead of 2 I have n? I tried this
X_new <- vector(mode = "list", length = n)
ind <- names(X[[1]])
for (i in 1:n) {
X[[i]] <- lapply(X, function(x) x$ind[i])
}
but it doesn't work, I just get null lists.