我已经发布了一个相关的问题(Create a new vector by appending elements between them in R)。我想知道是否可以用指定数量的元素(例如,从purr包中积累())来增加向量。
事实上,我正在研究一个包含16000个基因的载体。我试图编写一个for循环,在每次迭代时,应该从数据集中删除100个基因并进行聚类分析(对16000个基因进行聚类,用15900个基因进行聚类,用15800个基因进行聚类,等等)。我的想法是从向量中列出一个列表,其中每一个元素都是一个基因向量,增加100个基因(第一个元素100个,第二个元素200个,第三个元素300个,第160个元素,总共16000个基因)。通过累加(),我只能在以下两个元素之间逐个递增。有没有办法使它增加100乘以100?
再次感谢你们的帮助!
发布于 2022-06-20 19:31:47
而不是for循环,您可以使用while循环,每次构建一个新的列表。这并不是最有效的方法,但考虑到您的数据集大小,它应该能做到这一点。
下面是一些帮助您入门的代码:
# Create a list of values
my_list <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23)
# Get the length of your list
max_len <- length(my_list)
# While [max_len] is positive, create a new list of [max_len] elements and decrement [max_len] by some value (here, 10) for the next list
while (max_len > 0) {
new_list = my_list[1:max_len]
print(new_list)
max_len <- max_len - 10
}
希望这能帮上忙!
https://stackoverflow.com/questions/72683953
复制相似问题