Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 跟踪嵌套列表中的父列表_R_Loops_Nested - Fatal编程技术网

R 跟踪嵌套列表中的父列表

R 跟踪嵌套列表中的父列表,r,loops,nested,R,Loops,Nested,我有一个需要迭代的嵌套循环。我想转到列表的末尾(在本例中是父列表的第二项),如果该项不再是嵌套循环,则将其添加到列表中。因此,循环可能有许多级别的嵌套循环。现在,我只得到第二份名单作为回报。如何跟踪父列表 a <- list( x = list(1,2,3),y =list(4,5,6)) con=TRUE while(con){ i <-length(a) for(k in i:i){ if(!typeof(a[[k]])=="list"){ a[[k+1]] &l

我有一个需要迭代的嵌套循环。我想转到列表的末尾(在本例中是父列表的第二项),如果该项不再是嵌套循环,则将其添加到列表中。因此,循环可能有许多级别的嵌套循环。现在,我只得到第二份名单作为回报。如何跟踪父列表

a <- list( x = list(1,2,3),y =list(4,5,6))
 con=TRUE
 while(con){
 i <-length(a)
 for(k in i:i){
 if(!typeof(a[[k]])=="list"){
   a[[k+1]] <- "test"
   con=FALSE
  }else{
  a <- a[[k]]
   i <- length(a)
 }
 }
}


Expected Result:a <- list(x = list(1,2,3), y =list(4,5,6, "test"))
Result: a <- list(4,5,6,"test")
a
库(magrittr)
一个
库(magrittr)
A.
library(magrittr)
a <- list( x = list(1,2,3),y =list(4,5,6), z = 1)

temp <- lapply(a, typeof) %>% unlist
tempList <- (temp!="list")

if (sum(tempList) > 0) {
  a[[max(which(tempList == FALSE))]] %<>% append("test")
} else {
  a[[length(a)]] %<>% append("test")
}