Search code examples
rrep

Need help understandig the 'rep()' function


rep (2,5)
rep

Hello everyone, I am learning 'R' by watching a Udemy tutorial and I've been following along. Recently I learned seq() and rep() function. However, when I try to run the code written above I get an additional output. The code returns 2.2.2.2.2 and .Primitive("rep"). I am using Kaggle notebooks. Help me understand how this functions works, what is going wrong here, and what will happen if we provide multiple input as rep(2,3,4,5) or (1,2,3,4,6,8).


Solution

  • In R, rep is a function. It is designed to replicate its first argument a number of times equal to its second argument. Thus rep(2, 5) returns a vector of length 5 with each element as 2.

    In R, functions are also objects, and when you input a function's name, R will return the something that tries to be useful by showing that the input is a function and providing the expected arguments. The .Primitive("rep") part tells you that rep is a primitive function, part of the base R code.

    rep
    function (x, ...)  .Primitive("rep")
    

    In this case, rep requires at least one argument x, which the object to be replicated. The ... indicates that it can take a number of other optional arguments. To learn about them, you can access the help file for rep with ?rep.

    You can call rep with more arguments, but the behavior might not be what you expect.