3

Python has the *(...) syntactic sugar. Can you do this in R?

t = (2010, 10, 2, 11, 4, 0, 2, 41, 0)
dt = datetime.datetime(*t[0:7])

From here: https://stackoverflow.com/a/2238361/1007926

This allows each element of the tuple to be assigned to an argument of, in this case, the datetime function.

An analogous trick in R might look like this, if the syntax were the same as Python:

lims <- c(10,20)
my.seq <- seq(*lims)

I don't believe this exactly the same as "unpacking" used in this question:

>>> a, b, c = (1, 2, 3)

Is there a way to do it in R, as below?

a, b, c = c(1, 2, 3)

Python-like unpacking of numeric value in R

Community
  • 1
  • 1
Peter Becich
  • 989
  • 3
  • 14
  • 30
  • What exactly is the question here? What do you expect as the result? Not all of us know Python. – Rich Scriven Nov 29 '14 at 06:26
  • Okay. I think I've clarified a bit. Thanks – Peter Becich Nov 29 '14 at 06:42
  • 1
    Not clear yet. In `R`, the order of arguments to a function need not be fixed. Are you asking for this? --> function defined as `foo(x,y,z,k,l,m)` and you want to assign the values of an object `bar[1:6]` to the arguments of `foo` ? If so, take a look at `?formals` to see if it'll do what you want – Carl Witthoft Nov 29 '14 at 13:20

1 Answers1

2

The closest thing I can think of is do.call:

> lims <- c(10,20)
> do.call(seq, as.list(lims))
 [1] 10 11 12 13 14 15 16 17 18 19 20

But do note that there are some subtle differences in evaluation that may cause some function calls to be different than if you had called them directly rather than via do.call.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 2
    Can wrap that in a functional wrapper: `tun = function(f){function(...){do.call(f,as.list(...))}}` then `tun(seq)(lims)` works. – Spacedman Dec 03 '14 at 16:20