Function returning multiple values ​​in R

Say I have a function

f <- function(){

  # do things

  return(list(zoo = x, vector = y, integer = z))
}

When I call this function using

result <- f()

Is there a way to explicitly call return variables? Those. I have to call him using

vara <- result$vara
varb <- result$varb
varc <- result$varc

Since if this function returns many types of data (whether it be data frames, zoos, vectors, etc.). The reason I don't want to bind to a data frame is because not all variables are the same length.

+4
source share
1 answer

Use withorwithin

If the problem is that you don’t like typing result, you can use withor withinto access items within the list.

with(result, vara + varb * varc)

This is a good, standard way of doing things.

Use list2env

list2env.

 list2env(result, parent.frame())

, , . , R CMD .

attach

( ).

attach with.

attach(result)

result$. , , , , .

+3

All Articles