Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rep function in R

Tags:

r

rep

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!

like image 456
Vinayak Agarwal Avatar asked Aug 08 '12 18:08

Vinayak Agarwal


People also ask

How do you repeat a value in R?

To create a data frame with a column having repeated values, we simply need to use rep function and we can repeat the values in a sequence of the values passed or repeating each value a particular number of times.

How do you replicate a list in R?

The replication of list of a list can be created by using rep function. For example, if we have a list called x and we want to create five times replicated list of this list then we can use the code rep(list(x),5).

How do you define a function in R?

To declare a user-defined function in R, we use the keyword function . The syntax is as follows: function_name <- function(parameters){ function body } Above, the main components of an R function are: function name, function parameters, and function body.


1 Answers

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.

like image 108
GSee Avatar answered Sep 18 '22 20:09

GSee