在函数中返回不同的数据帧 - R.

时间:2021-12-15 12:46:00

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

是否可以从一个函数返回4个不同的数据框?

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?

我不想使用list(carFile,carNames,carYear)。有没有办法返回3个数据帧而不先将它们返回列表?

2 个解决方案

#1


21  

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

R不支持多个返回值。你想做的事情如下:

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.

是吗?好吧,你不能。您收到R无法返回多个值的错误。

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.

请注意,在您的示例中,carYear不是数据框 - 它是列名的字符向量。

#2


3  

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.

如果您使用它,那么将在调用环境中创建carFile。正如Spacedman所说,你只能从你的功能中返回一个东西,而干净的解决方案是去列表。

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

将会做这项工作,但正如SpacedMan所说

when you do that, kittens die

当你这样做时,小猫会死

#1


21  

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

R不支持多个返回值。你想做的事情如下:

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.

是吗?好吧,你不能。您收到R无法返回多个值的错误。

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.

请注意,在您的示例中,carYear不是数据框 - 它是列名的字符向量。

#2


3  

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.

如果您使用它,那么将在调用环境中创建carFile。正如Spacedman所说,你只能从你的功能中返回一个东西,而干净的解决方案是去列表。

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

将会做这项工作,但正如SpacedMan所说

when you do that, kittens die

当你这样做时,小猫会死