2

I am using the example of calculating the length of the arc around a circle and the area under the arc around a circle based on the radius of the circle (r) and the angle of the the arc(theta). The area and the length are both based on r and theta, and you can calculate them simultaneously in python.

In python, I can assign two values at the same time by doing this.

    from math import pi
    def circle_set(r, theta):
        return theta * r, .5*theta*r*r

    arc_len, arc_area = circle_set(1, .5*pi) 

Implementing the same structure in R gives me this.

    circle_set <- function(r, theta){
        return(theta * r, .5 * theta * r *r)
        }

    arc_len, arc_area <- circle_set(1, .5*3.14)

But returns this error.

arc_len, arc_area <- circle_set(1, .5*3.14)

Error: unexpected ',' in "arc_len,"

Is there a way to use the same structure in R?

polka
  • 1,383
  • 2
  • 25
  • 40
  • these are two separate values, arc_len != arc_area – polka May 09 '16 at 19:09
  • 2
    I have no clue what that python code is doing so you might need to explain what it is you are attempting to do. – Rich Scriven May 09 '16 at 19:09
  • ok, done. let me know if my explanation is okay. – polka May 09 '16 at 19:12
  • 2
    To help OP with searching, the Python feature is called tuple unpacking. The R way of doing things is to rely on lists more as in Gregor's answer; i.e. use a list `arc_properties` instead of separate `arc_len`, `arc_area` variables. Note that this is less troublesome than the equivalent would be in Python because list elements can be named (so they have some dictionary-like properties) and because they are not mutable in place - R has copy-on-modify semantics – Calum You Mar 02 '19 at 04:27
  • definitely non "off-topic", but possible duplicate of: https://stackoverflow.com/q/7519790/592355 – xerx593 Mar 02 '19 at 09:10

2 Answers2

6

No, you can't do that in R (at least, not in base or any packages I'm aware of).

The closest you could come would be to assign objects to different elements of a list. If you really wanted, you could then use list2env to put the list elements in an environment (e.g., the global environment), or use attach to make the list elements accessible, but I don't think you gain much from these approaches.

If you want a function to return more than one value, just put them in a list. See also r - Function returning more than one value.

You can assign multiple variables the same value as below. Even here, I think the code is unusual and less clear, I think this outweighs any benefits of brevity. (Though I suppose it makes it crystal clear that all of the variables are the same value... perhaps in the right context it makes sense.)

x <- y <- z <- 1
# the above is equivalent to
x <- 1
y <- 1
z <- 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
1

As Gregor said, there's no way to do it exactly as you said and his method is a good one, but you could also have a vector represent your two values like so:

# Function that adds one value and returns a vector of all the arguments.
plusOne <- function(vec) {

  vec <- vec + 1
  return(vec)

}

# Creating variables and applying the function.
x <- 1
y <- 2
z <- 3
vec <- c(x, y, z)
vec <- plusOne(vec)

So essentially you could make a vector and have your function return vectors, which is essentially filling 3 values at once. Again, not what you want exactly, just a suggestion.

giraffehere
  • 1,118
  • 7
  • 18