如何仅通过for循环创建向量? (向量在主体中指定) [英] How can I create a vector by only using for loop? (vector is specified in the body)
本文介绍了如何仅通过for循环创建向量? (向量在主体中指定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(1,2,2,3,3,3,4,4,4,4,...,n,...,n)
(1,2,2,3,3,3,4,4,4,4,...,n,...,n)
我想通过for循环制作上述向量,但不使用rep函数或其他函数.在stackoverflow中问这个问题可能不是一个好问题,但是由于我是R的新手,所以我敢在这里寻求帮助.
I want to make the above vector by for loop, but not using rep function or the other functions. This may not be good question to ask in stackoverflow, but since I am a newbie for R, I dare ask here to be helped.
(您可以假设向量的长度为10)
(You can suppose the length of the vector is 10)
推荐答案
使用for
循环,可以使用
n <- 10
out <- c()
for(i in seq_len(n)){
for(j in seq_len(i)) {
out <- c(out, i)
}
}
在R
中,否则,可以这样做
In R
, otherwise, this can be done as
rep(seq_len(n), seq_len(n))
这篇关于如何仅通过for循环创建向量? (向量在主体中指定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文