R嵌套循环只返回最后一次迭代 [英] R nested loop returning only the last iteration

查看:250
本文介绍了R嵌套循环只返回最后一次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用嵌套for循环来复制实验室存储系统中使用的81位cryobox。下面的代码说明了使用3位盒子的问题:

$ $ $ $ $ $ $ $ c $ u $ u_random_df < - as.data.frame(c(seq(从= 10到= 12 by = 1)))
boxcells< - vector()
cell_placeholder< - as.data.frame(c(seq(from = 1,to = 3通过= 1)))
为(I在1:3){
#boxcells< - paste0( NEW,的sprintf( %04D,as.numeric(urine_random_df [I, ])))
。对于(j在1:nrow(cell_placeholder)){
boxcells< - C(boxcells,糊剂(paste0( NEW,的sprintf( %04D,as.numeric (尿尿液_df [i,]))),cell_placeholder [j,],sep = - ))
}

}


boxcells < - data.frame(boxcells)
names(boxcells)< - box cell
boxcells

上面的结果是:

pre code $ box
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

但是,我想将单元格分组到它们各自的框中,如下所示:

pre>
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 .nu​​meric(urine_random_df [i,])))在外层循环中。当我用这个代码重新运行代码时,我得到的只是最后一个框:

pre code $ box b
1 NEW0012
2 NEW0012-1
3 NEW0012-2
4 NEW0012-3

循环的每一次迭代都会清除最后一个循环,在完成整个循环后,只剩下最后一个循环。我在这里找到了一个现有的线程,这表明在循环之外移动初始化语句。然而,在这种情况下,初始化语句 urine_random_df ... boxcells ... cell_placeholder ... 已经在循环之外。想一想

解决方案

我可以考虑非常少见的情况,当你为在R中循环,即使单个循环也是非常罕见的。



类似

 温度<  -  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 $,












$ $ b

  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_cel 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


I am trying to replicate 81-place cryoboxes used in lab storage system using a nested for loop. The following code illustrates the problem using 3-place boxes:

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

The result of above is:

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

However, I would like to group the cells under their respective boxes like so:

   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

I tried to achieve this by adding boxcells <- paste0("NEW", sprintf("%04d", as.numeric(urine_random_df[i,]))) in the outer loop. When I re-ran the code with this piece, I get only the last box like so:

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

It appears each iteration of the loop erases the last such that upon completion of the entire loop, only the last box remains. I found an existing thread here which suggests moving the "initialisation statements" outside the loop. However, in this case, the initialisation statements urine_random_df..., boxcells... and cell_placeholder... are already outside the loop. Thoughts?

解决方案

I can think of very rare situations when you would do a nested for loop in R, even single for loop is very rare.

I would solve this by doing something like

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])))

Which will return

   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

If you don't like the order, you could reorder by

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

这篇关于R嵌套循环只返回最后一次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆