0

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)

3
  • Then are you just looking for rep? Commented Apr 26, 2015 at 13:47
  • In other words, simply rep(labels, ccl$size) Commented Apr 26, 2015 at 13:48
  • You need to learn that the first argument to 'matrix' are the Values and the second arg is the number of rows. Commented Apr 26, 2015 at 13:55

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.