Username
Username

Reputation: 3663

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?

Upvotes: 1

Views: 9311

Answers (1)

989
989

Reputation: 12935

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"

Upvotes: 5

Related Questions