Search code examples
rfunctionrep

Function in rep() function


Why is

rep(message("TEST"),3)
rep(c(message("TEST")),3)

>
TEST
NULL
> 

not the same as

c(message("TEST"),message("TEST"),message("TEST"))

>
TEST
TEST
TEST
NULL
> 

How do I then get the second result using the rep() function?

EDIT:

Very "conveniently" we could use:

invisible(lapply((rep(c(quote(message("TEST"))),3)),eval))
> 
TEST
TEST
TEST
> 

Solution

  • From ?rep:

    ‘rep’ replicates the values in ‘x’.

    The value from message("TEST") is NULL.

    You can check this with e.g.

    m <- message("TEST");
    m;
    #NULL
    rep(m, 3);
    #NULL
    rep(NULL, 3);
    #NULL
    

    As to your second question, see @docendodiscimus' comment.