surya ambati
surya ambati

Reputation: 104

Loop with particular assigned numbers

May be below question might be very basic but I searched internet but did not get the answer. So I felt stack Overflow is the right place.

I would like to run for loop in "R" with specific numbers.

ex: aa <- c(4,6,8,9)

for (i in aa) print("test")

Note: when I run loop it will run/print for 4 times only.

But I want numbers in vector should be parameter for loop

Expected:

ex: aa <- c(4,6,8,9)

for (i in aa) print("test")

in place of aa we should get 4 . so test will print 4 times. Later 6 then again it need to print 6 times etc.

Upvotes: 0

Views: 32

Answers (1)

akrun
akrun

Reputation: 887183

We can use rep

for(i in aa) print(rep("test", i))

Or may be with strrep

for(i in aa) print(strrep("test", i))

Upvotes: 2

Related Questions