18

Using the command paste in R, I wanted to use both arguments sep and collapse, but you cannot abbreviate collapse to coll or even collaps. Yet for other functions partial abbreviation works.

For example:

paste(letters, colla=", ")
# [1] "a , " "b , " "c , " "d , " "e , " "f , " "g , " "h , " "i , " "j , " "k , " "l , " "m , " "n , " "o , " "p , " "q , " "r , "
[19] "s , " "t , " "u , " "v , " "w , " "x , " "y , " "z , "
paste(letters, collapse=", ")
# [1] "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z"

There are no other arguments to collapse that start with coll, which would interfere with the partial argument matching.

Why must I type out the entire argument name when calling paste, when I do not have to for other functions?

Megatron
  • 15,909
  • 12
  • 89
  • 97
user1879178
  • 181
  • 2
  • 4
    I would actually really not use this kind of abbreviation as it is harder to read and can have unforseen consequences as the expansion could lead to the wrong parameter matching. – Paul Hiemstra Dec 05 '12 at 15:11

2 Answers2

23

I believe it's the ... in paste that causes that you have to use exact argument matching. Specifically, the fact that ,collapse comes after the ... in the argument list.

Demonstration:

f1 <- function(x, collapse) cat("collapse",collapse)
f2 <- function(..., collapse) cat("collapse",collapse)
f3 <- function(collapse, ...) cat("collapse",collapse)

> f1(c="test",1)
collapse test
> f2(1,c="test")
Error in base::cat(...) : argument "collapse" is missing, with no default
> f2(1,collapse="test")
collapse test
> f3(c="test",1)
collapse test
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • 1
    This is my understanding as well. There is a warning "somewhere" in the R documentation that any named argument following `...` cannot be shortened. – Carl Witthoft Dec 05 '12 at 15:30
  • 7
    It's documented in [section 4.3.2 Argument matching](http://cran.r-project.org/doc/manuals/R-lang.html#Argument-matching) of [R Language Definition](http://cran.r-project.org/doc/manuals/R-lang.html). – Joshua Ulrich Dec 05 '12 at 16:00
  • 1
    So the hacker in me suggests `mypaste <-function(sep = " ", collapse = NULL,...) paste(sep,collapse,...)` :-) – Carl Witthoft Dec 05 '12 at 17:48
2

A wrapper function might be helpful, much like paste0

p <- function(..., s=" ", clap=NULL) {   # or whichever abbreviation you prefer. I originally had `col`, but that was dumb. 
   paste(..., sep=s, collapse=clap)
}

p0 <- function(...,  clap=NULL) {
   paste(..., sep="", collapse=clap)
}

eg :

p(c("hello", "world"), c("abc", "123"), clap="$")
# [1] "hello abc$world 123"


p0(c("hello", "world"), c("abc", "123"), clap="$")
# [1] "helloabc$world123"
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
  • While this might be of use to the OP, I don't think it really answers the question of *why* the abbreviation doesn't work, which Ari's answer does... – A5C1D2H2I1M1N2O1R2T1 Dec 05 '12 at 18:55
  • @Carl, thanks for the cleanup. Ananda: I think you're right about the exactness, still this may be helpful to the OP. I think Ari did a great job answering the why – Ricardo Saporta Dec 05 '12 at 19:00