如何使用“while"循环附加到向量? [英] How do I append to a vector using a `while` loop?
本文介绍了如何使用“while"循环附加到向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用循环制作一个向量.
I want to make a vector using a loop.
这是我的 R 代码:
vec_teamCodes <- c()
x <- 0
while (x < 10) {
append(vec_teamCodes,"Hello")
x <- x+1
}
但是当我运行它时,vec_teamCodes()
仍然是 NULL
.
But when I run it, vec_teamCodes()
remains NULL
.
为什么?如何修复我的代码?
Why? How do I fix my code?
推荐答案
试试这个:
vec_teamCodes <- c()
x <- 0
while (x < 10) {
vec_teamCodes <- c(vec_teamCodes,"Hello")
# OR
# vec_teamCodes <- append(vec_teamCodes,"Hello")
x <- x+1
}
[1] "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" "Hello" "Hello"
这篇关于如何使用“while"循环附加到向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文