You can do that using a recursive function.
rec.list <- function(len){
if(length(len) == 1){
vector("list", len)
} else {
lapply(1:len[1], function(...) rec.list(len[-1]))
}
}
l <- rec.list(c(2, 3, 3, 4, 2, 3, 3))
Or perhaps with a 7-d list array? It might look bizarre at first, but it is a perfectly valid data structure.
l <- vector("list", 2*3*3*4*2*3*3)
dim(l) <- c(2, 3, 3, 4, 2, 3, 3)
l[[1,1,1,1,1,1,1]] <- "content"
Answer from Backlin on Stack OverflowYou can do that using a recursive function.
rec.list <- function(len){
if(length(len) == 1){
vector("list", len)
} else {
lapply(1:len[1], function(...) rec.list(len[-1]))
}
}
l <- rec.list(c(2, 3, 3, 4, 2, 3, 3))
Or perhaps with a 7-d list array? It might look bizarre at first, but it is a perfectly valid data structure.
l <- vector("list", 2*3*3*4*2*3*3)
dim(l) <- c(2, 3, 3, 4, 2, 3, 3)
l[[1,1,1,1,1,1,1]] <- "content"
Set some kind of parameters:
window_front <- 100
horizon <- 10
Create an empty nested list:
sub_horizon <- vector("list", 27)
for (i in 1:length(sub_horizon)) {
sub_horizon[[i]] <- vector("list", window_front)
for (m in 1:length(sub_horizon[[i]])) {
sub_horizon[[i]][[m]] <- vector("list", horizon)
}
}
There's another way to assign to a list, using my_list[[name or number]] <-. If you really want to do that in a loop, just looping over things with names like iter1, iter2, ...
A <- list()
n_iter <- 2
for (i in 1:n_iter){
iname <- paste("iter",i,sep="")
A[[iname]] <- get(iname)
}
As @mnel pointed out, dynamically growing a list is inefficient. The alternative is, I think, to use lapply:
n_iter <- 2
inames <- paste("iter",1:n_iter,sep="")
names(inames) <- inames
A <- lapply(inames,get)
This can also be done with a data frame, which would be a better format if your sublists always have two elements, each having a consistent class (item1 being numeric and item 2 being character).
n_iter <- 2
DF <- data.frame(item1=rep(0,n_iter),item2=rep("",n_iter),stringsAsFactors=FALSE)
for (i in 1:n_iter){
iname <- paste("iter",i,sep="")
DF[i,] <- get(iname)
rownames(DF)[i] <- iname
}
# item1 item2
# iter1 1 a
# iter2 1 b
However, that's a pretty ugly way of doing things; things get messy pretty quickly when using get. With your data structure, maybe you want to create iter1 and iter2 in a loop and immediately embed them into the parent list or data frame?
n_iter = 10
DF <- data.frame(item1 = rep(0,n_iter), item2 = rep("",n_iter))
for (i in 1:n_iter){
... do stuff to make anum and achar ...
DF[i,"item1"] <- anum
DF[i,"item2"] <- achar
}
Where anum and achar are the values of item1 and item2 you want to store from that iteration. Elsewhere on SO, they say that there is an alternative using the data.table package that is almost 10x as fast/efficient as this sort of data-frame assignment.
Oh, one last idea: if you want to put them in a list first, you can easily convert to a data frame later with
DF <- do.call(rbind.data.frame,A)
This gets you the equivalent of your All
c(iter1=list(iter1), iter2=list(iter2))
> identical(c(iter1=list(iter1), iter2=list(iter2)), All)
[1] TRUE
Let's say you'd like to add a third list to All:
c(All, list(iter3=iter3))
If you don't care for the list names, it looks a little cleaner
c(list(iter1), list(iter2))
Altough your example isn't reproducible, I get a list of lists with the following similar code:
potential_dups <- rep(list(list()), 10)
nearest10 <- matrix(rnorm(100), nrow=10)
for (i in 1:10) {
for (j in 1:10) {
if (nearest10[i, j] < 0.35 & nearest10[i, j] > 0) {
potential_dups[[i]] <- append(potential_dups[[i]], nearest10[i, j])
}
}
}
To remove empty lists you can do this:
potential_dups[sapply(potential_dups, function(x) length(x) > 0)]
Here is a nicer (better readable and more efficient) way:
mat <- nearest10
mat[mat >= 0.35 | mat <= 0] <- NA
potential_dups <- apply(mat,1,function(x) as.list(na.omit(x)))
However, I can't imagine why you want this output. It doesn't seem the most useful. maybe you could use the following instead?
potential_dups <- apply(mat,1,function(x) c(na.omit(x)))
We can just do assignment
lwa_res_list[[1]] <- current_res
In the OP's code, if the list was initialized as
lwa_res_lst <- list()
Or using OP's code
lwa_res_lst <- vector(mode = "list", length = 0)
the code should work
lwa_res_lst <- list.append(lwa_res_lst, current_res)
lwa_res_lst
#[[1]]
#[1] 1
current_res <- 1 #any number you want for list
lwa_res_lst <- vector(mode = "list", length = current_res)
lwa_res_lst
class(lwa_res_lst)