How to specify arguments after dots ... in R?

r

Solution

I was waiting to see if any of the first commenters were going to answer, but they haven't so I will.

Since in the definition of `c()` the first argument is `...`, you must completely name any arguments that follow for them to work correctly. I say arguments plural and not argument singular because there is actually a second, undocumented argument in `c()`. See Why does function c() accept an undocumented argument? for more on that. For arguments that come before `...`, you can use partial argument matching, but for those after `...` you must write the complete argument name.

What's happening in your example is that you are concatenating the numeric value of `TRUE` (which is 1) to the vector `1:5`

as.numeric(TRUE)            ## numeric value of TRUE
# [1] 1
c(1:5, TRUE)                ## try no arg name, wrong result
# [1] 1 2 3 4 5 1
c(1:5, rec=TRUE)            ## try partial arg matching, wrong result
#                    rec 
#  1   2   3   4   5   1 

Furthermore, `recursive` really does nothing in the manner that you're attempting to use it.

c(1:5)
# [1] 1 2 3 4 5
c(1:5, recursive=TRUE)      ## 1:5 not recursive, same result
# [1] 1 2 3 4 5
is.recursive(c(1:5))
# [1] FALSE

It's more for concatenating list items, or any other recursive objects (see `?is.recursive`).

(x <- as.list(1:5))
# [[1]]
# [1] 1
# 
# [[2]]
# [1] 2
# 
# [[3]]
# [1] 3
# 
# [[4]]
# [1] 4
# 
# [[5]]
# [1] 5
is.recursive(x)
# [1] TRUE
c(x, recursive=TRUE)        ## correct usage on recursive object x
# [1] 1 2 3 4 5

Problem

This probably comes down to 'how to pass optional parameters to R functions'. Take `c()` for example. It's definition is: ``` c(..., recursive = FALSE) ``` However, if I use it like this `c(1:5, TRUE)` it gives `[1] 1 2 3 4 5 1` which is perfectly understandable, but also a bit strange since I'd expect it to be simple to figure out. I guess it IS simple and I'm simply not seeing the whole thing. Cheers for answering and not raging. I googled and searched SO, but probably got too many wrong answered and gave up. EDIT: edited due to illogical example.

Original source

Related problems