R嵌套循环仅返回最后一次迭代

时间:2014-07-01 17:41:54

标签: r loops for-loop nested-loops

我正在尝试使用嵌套for循环复制实验室存储系统中使用的81位冷冻箱。以下代码说明了使用3位框的问题:

urine_random_df <- as.data.frame(c(seq(from = 10, to = 12, by = 1)))
boxcells <- vector()
cell_placeholder <- as.data.frame(c(seq(from = 1, to = 3, by = 1)))
for (i in 1: 3){
        #boxcells <- paste0("NEW", sprintf("%04d", as.numeric(urine_random_df[i,])))
        for (j in 1: nrow(cell_placeholder)){
                boxcells <- c(boxcells, paste(paste0("NEW", sprintf("%04d", as.numeric(urine_random_df[i,]))), cell_placeholder[j,], sep = "-"))        
        }

}


boxcells <- data.frame(boxcells)
names(boxcells) <- "box cells"
boxcells

以上结果是:

box cells
1 NEW0010-1
2 NEW0010-2
3 NEW0010-3
4 NEW0011-1
5 NEW0011-2
6 NEW0011-3
7 NEW0012-1
8 NEW0012-2
9 NEW0012-3

但是,我想将细胞分组在各自的盒子下面,如下:

   box cells
1  NEW0010
2  NEW0010-1
3  NEW0010-2
3  NEW0010-3
4  NEW0011
5  NEW0011-1
6  NEW0011-2
7  NEW0011-3
8  NEW0012
9  NEW0012-1
10 NEW0012-2
11 NEW0012-3

我尝试通过在外部循环中添加boxcells <- paste0("NEW", sprintf("%04d", as.numeric(urine_random_df[i,])))来实现此目的。当我用这篇文章重新运行代码时,我只得到最后一个框:

  box cells
1   NEW0012
2 NEW0012-1
3 NEW0012-2
4 NEW0012-3

看起来循环的每次迭代都会删除最后一次,这样在完成整个循环后,只剩下最后一个框。我找到了existing thread here,建议在循环外移动“初始化语句”。但是,在这种情况下,初始化语句urine_random_df...boxcells...cell_placeholder...已经在循环之外。想法?

3 个答案:

答案 0 :(得分:0)

我可以想到在R中进行嵌套for循环时非常罕见的情况,即使单个for循环非常罕见。

我会通过做类似

的事来解决这个问题
temp <- expand.grid(sprintf("%04d", as.numeric(urine_random_df[,1])), c("", paste0("-",cell_placeholder[, 1])))
boxcells <- data.frame(box_cells = paste0("NEW", paste0(temp[, 1], temp[, 2])))

将返回

   box_cells
1    NEW0010
2    NEW0011
3    NEW0012
4  NEW0010-1
5  NEW0011-1
6  NEW0012-1
7  NEW0010-2
8  NEW0011-2
9  NEW0012-2
10 NEW0010-3
11 NEW0011-3
12 NEW0012-3

如果您不喜欢订单,可以按

重新订购
boxcells <- data.frame(box_cells = boxcells[order(as.numeric(substr(boxcells$box_cells, 6,7))), ])

   box_cells
1    NEW0010
2  NEW0010-1
3  NEW0010-2
4  NEW0010-3
5    NEW0011
6  NEW0011-1
7  NEW0011-2
8  NEW0011-3
9    NEW0012
10 NEW0012-1
11 NEW0012-2
12 NEW0012-3

答案 1 :(得分:0)

我认为这里的复杂性来自于将循环的输出收集为向量而不是列表。

这里使用列表,然后取消列出并转换为数据帧。输出完全符合要求

urine_random_df <- as.data.frame(c(seq(from = 10, to = 12, by = 1)))
boxcells <- list()
cell_placeholder <- as.data.frame(c(seq(from = 1, to = 3, by = 1)))
n <- nrow(cell_placeholder)
for (i in 1:n){
  tmp <- vector()
tmp <- c(paste0("NEW", sprintf("%04d", as.numeric(urine_random_df[i,]))))
      for (j in 1:n){
        tmp <- c(tmp, paste(paste0("NEW", sprintf("%04d", as.numeric(urine_random_df[i,]))), cell_placeholder[j,], sep = "-"))        
      }
      boxcells[[i]] <- tmp 
}


boxcells <- data.frame(unlist(boxcells))
names(boxcells) <- "box cells"
boxcells

给出了:

 box cells
1    NEW0010
2  NEW0010-1
3  NEW0010-2
4  NEW0010-3
5    NEW0011
6  NEW0011-1
7  NEW0011-2
8  NEW0011-3
9    NEW0012
10 NEW0012-1
11 NEW0012-2
12 NEW0012-3

答案 2 :(得分:0)

如果您想更好地分组结果,请选择其他结构来存储结果。这里有2种方法可以简单地解决您的问题:

nn <- paste0('NEW',sprintf("%04d",10:12))

使用列表

setNames(lapply(nn,function(x){
  paste(x,1:3,sep='-')
}),nn)

$NEW0010
[1] "NEW0010-1" "NEW0010-2" "NEW0010-3"

$NEW0011
[1] "NEW0011-1" "NEW0011-2" "NEW0011-3"

$NEW0012
[1] "NEW0012-1" "NEW0012-2" "NEW0012-3"

使用data.frame

transform(expand.grid(nn,1:3),Var2=paste(Var1,Var2,sep='-'))

     Var1      Var2
1 NEW0010 NEW0010-1
2 NEW0011 NEW0011-1
3 NEW0012 NEW0012-1
4 NEW0010 NEW0010-2
5 NEW0011 NEW0011-2
6 NEW0012 NEW0012-2
7 NEW0010 NEW0010-3
8 NEW0011 NEW0011-3
9 NEW0012 NEW0012-3
相关问题