tlorin
tlorin

Reputation: 1150

Initialize multiple empty vectors at once

I want to initialize several identical (empty) vectors before feeding them into a loop.

For instance, what I do is

a <- c()
b <- c()
c <- c()
d <- c()
...

Is there a more compact way to do this (one-liner ideally)?

I would imagine something like:

(a, b, c, d, ...) <- c() # not working 

Upvotes: 0

Views: 2328

Answers (1)

zx8754
zx8754

Reputation: 56159

Maybe:

a <- b <- c <- c()

But as mentioned in the comments, we should probably re-think our approach.

David Arenburg:

There is no need to initialize empty strings in R. You usually will pre-allocate a vector of some length in order to populate it within a loop. I have a feeling that this is exactly what you are going to do: create empty strings and then grow them in a loop.

This is not a recommended approach in R

Upvotes: 2

Related Questions