Benjamin Zaugg
Benjamin Zaugg

Reputation: 1

creation of a vector out of matrixes using a loop

Im trying to create a vector by combining multiple matrixes using a loop. If I do it manually it looks like this:

vector = c(
           matrix(labels[1],ccl$size[1]), 
           matrix(labels[2],ccl$size[2]), 
           matrix(labels[3],ccl$size[3]),
           matrix(labels[4],ccl$size[4]),
           matrix(labels[5],ccl$size[5]))

labels is a vector with a given number of elements, as is ccl$size. the problem is that no loop seems to accept any substring of the function as a valuable input.

edit: I tried

c(for(i in repeats) 
{matrix(labels[i],ccl$size[i]),}
)

edit2:

inputs labels: c(2,1,3)

ccl$size: c(12,10,7)

desired output c(2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3)

Upvotes: 0

Views: 34

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193527

You're looking for rep:

v1 <- c(2,1,3)
v2 <- c(12, 10, 7)

rep(v1, v2)
#  [1] 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3

Upvotes: 2

Related Questions