Вопрос

When I perform:

rep(1:4, rep(4,4))

I get

1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 

which is expected. But then when I try to fix the length to 16(which is the length of the output) as follows:

rep(1:4, rep(4,4), length.out = 16)

I get

1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

which is weird. I think both of these commands should perform the same function. Can someone please help?

Thanks!

Это было полезно?

Решение

From ?rep

‘length.out’ may be given in place of ‘times’, in which case ‘x’ is repeated as many times as is necessary to create a vector of this length. If both are given, ‘length.out’ takes priority and ‘times’ is ignored.

Другие советы

rep(1:4,,rep(4,4),length.out=16) will give the result you are looking for. A simpler way to write this is rep(1:4,,16,4).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top