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!
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With