on-internet
← Community

R: for loop iteration with seq_along vectors of a list

Stack Overflow ·

consider the following code: listLength <- sample(1:10, 1) vectorLength <- sample(1:10, 1) x <- vector("list") for(i in 1:listLength){ x[[i]] <- sample(1:10, vectorLength) } transform <- function(x){ result <- vector("list") for(i in seq_along(x[[1]])){ temp <- vector("numeric") for(j in seq_along(x)){ temp <- c(temp, x[[j]][i]) } result[[i]] <- temp } return(result) } and finally y <- transform(x) x is a list of random length , the vector elements of x have a random, but always equal length. The transform function returns a new list y, which is a list of vectorLength with vectors of listLengt

Comments

Sign in or sign up to comment and upvote.

No comments yet.