Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning different data frames in a function - R

Is it possible to return 4 different data frames from one function?

Scenario:

I am trying to read a file, parse it, and return some parts of the file.

My function looks something like this:

parseFile <- function(file){

     carFile <- read.table(file, header=TRUE, sep="\t")

     carNames <- carFile[1,]
     carYear  <- colnames(carFile)

     return(list(carFile,carNames,carYear))
}

I don't want to have to use list(carFile,carNames,carYear). Is there a way return the 3 data frames without returning them in a list first?

like image 841
Sheila Avatar asked Nov 24 '12 05:11

Sheila


People also ask

How do I return multiple Dataframes in a function in R?

Well, you can't. You get an error that R cannot return multiple values. You've already found the solution - put them in a list and then get the data frames from the list. This is efficient - there is no conversion or copying of the data frames from one block of memory to another.

Can a function return two Dataframes in R?

You can only return a single object from a function in R; if you need to return multiple objects, you need to return a list containing those objects, and extract them from the list when you return to the calling environment.

Can R function return multiple values?

Multiple Returns The return() function can return only a single object. If we want to return multiple values in R, we can use a list (or other objects) and return it.


2 Answers

R does not support multiple return values. You want to do something like:

foo = function(x,y){return(x+y,x-y)}
plus,minus = foo(10,4)

yeah? Well, you can't. You get an error that R cannot return multiple values.

You've already found the solution - put them in a list and then get the data frames from the list. This is efficient - there is no conversion or copying of the data frames from one block of memory to another.

This is also logical, the return from a function should conceptually be a single entity with some meaning that is transferred to whatever function is calling it. This meaning is also better conveyed if you name the returned values of the list.

You could use a technique to create multiple objects in the calling environment, but when you do that, kittens die.

Note in your example carYear isn't a data frame - its a character vector of column names.

like image 62
Spacedman Avatar answered Oct 12 '22 02:10

Spacedman


There are other ways you could do that, if you really really want, in R.

assign('carFile',carFile,envir=parent.frame())

If you use that, then carFile will be created in the calling environment. As Spacedman indicated you can only return one thing from your function and the clean solution is to go for the list.

In addition, my personal opinion is that if you find yourself in such a situation, where you feel like you need to return multiple dataframes with one function, or do something that no one has ever done before, you should really revisit your approach. In most cases you could find a cleaner solution with an additional function perhaps, or with the recommended (i.e. list).

In other words the

envir=parent.frame()

will do the job, but as SpacedMan mentioned

when you do that, kittens die

like image 27
Slak Avatar answered Oct 12 '22 00:10

Slak