error message: invalid 'times' argument

my matrix is:

d1
a b
[1,] 1 2
[2,] 2 2
[3,] 4 1

If I do the following I got the result:

rep(1,d1[1,2])
[1] 1 1
rep(1,d1[2,2])
[1] 1 1
rep(1,d1[3,2])
[1] 1

But why getting invalid 'times' argument for the following:

j=c(1:3)

j
[1] 1 2 3
rep(1,d1[j,2])
Error in rep(1, d1[j, 2]) : invalid 'times' argument

The help file says the following about the times argument:

`an integer-valued vector giving the (non-negative) number of times to repeat each element if of length `length(x)`, or to repeat the whole vector if of length 1. Negative or `NA` values are an error. A `double` vector is accepted, other inputs being coerced to an integer or double vector.`

So it can be either of length 1 or the same length as the argument x, which is one in this case. If you need to repeat the 1 for 1, 2 and 3 times use

``````rep(1, sum(j))
``````

I knew using sum(j) would work.
But not understanding my mistake while trying the other way.

My goal is to repeat any number with times=the call cell of a column.
I tried the following and got new error:

res=NULL
for (i in 1:3){res[i:d1[i,2]]=rep(1,d1[i,2])}
Warning message:
In res[i:d1[i, 2]] <- rep(1, d1[i, 2]) :
number of items to replace is not a multiple of replacement length