简体   繁体   中英

How do I append to a vector using a `while` loop?

I want to make a vector using a loop.

Here's my R code:

vec_teamCodes <- c()
x <- 0
while (x < 10) {
  append(vec_teamCodes,"Hello")
  x <- x+1
}

But when I run it, vec_teamCodes() remains NULL .

Why? How do I fix my code?

Try this:

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM