Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign same length to multiple vectors

Tags:

r

vector

How can I assign the same length n to multiple vectors, in one go?

At the moment I am stuck doing

length(vector_1) <- n
length(hello) <- n
....
....
length(cat) <- n

Many thanks

like image 535
Julia Avatar asked Mar 20 '26 02:03

Julia


1 Answers

If your vectors need to be the same length then it is likely that they are all related somehow. If they are related then it is usually better to collect them all together into a single object rather than having them as individual vectors in the global workspace. One option would be to put them all in a list together. Once in the list you can loop through the list (for loop or `lapply' function) and assign the length to each element. A data frame (which is stored as a list, so the above would work the same) is another way to store vectors that requires them to be the same length.

like image 187
Greg Snow Avatar answered Mar 21 '26 16:03

Greg Snow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!