为什么约会返回列表而不是数据框或数组?

时间:2022-02-23 21:22:58

It makes no sense to me that approx would return a list, since - if I understand it correctly - the two elements of the list that it returns are guaranteed to be numeric and of equal length. Given that, it would seem to make more sense to return an array or a data frame.

对我来说,约会返回一个列表是没有意义的,因为 - 如果我理解正确的话 - 它返回的列表中的两个元素保证是数字且长度相等。鉴于此,返回数组或数据框似乎更有意义。

I'm writing some functions using approx, and this makes me thing that I don't fully understand how it works. Is there a situation in which a list is necessary, or better?

我正在使用约来编写一些函数,这让我觉得我不完全理解它是如何工作的。是否有必要或更好的清单?

1 个解决方案

#1


5  

Functions take pairlists as arguments. Generally lists are used to pass arguments to lattice graphics, which was the dominant high-level graphics environment before ggplot and descendants came along. Lists are also used to pass parameters to control arguments in many functions. In this case you would expect the x and y lengths to be the same, so it could be a dataframe, but there is no particular reason to demand such a structure, and the base functions points and lines will accept named lists and properly allocate them to their argument lists.

函数将pairlists作为参数。通常,列表用于将参数传递给晶格图形,这是ggplot和后代出现之前的主要高级图形环境。列表还用于传递参数以控制许多函数中的参数。在这种情况下,您可以期望x和y长度相同,因此它可以是数据帧,但没有特别的理由要求这样的结构,并且基本功能点和线将接受命名列表并正确分配它们到他们的论点清单。

( I do not think a matrix could get properly distributed to the x and y arguments of the base plotting routines even of there were row or column names that matched. Matrices are really folded vectors.)

(我不认为矩阵可以正确地分配到基本绘图例程的x和y参数,即使有匹配的行或列名称。矩阵实际上是折叠向量。)

So I think the answer is that passing lists is most consistent with how functional programming is implemented in R.

所以我认为答案是传递列表与函数编程在R中的实现方式最为一致。

#1


5  

Functions take pairlists as arguments. Generally lists are used to pass arguments to lattice graphics, which was the dominant high-level graphics environment before ggplot and descendants came along. Lists are also used to pass parameters to control arguments in many functions. In this case you would expect the x and y lengths to be the same, so it could be a dataframe, but there is no particular reason to demand such a structure, and the base functions points and lines will accept named lists and properly allocate them to their argument lists.

函数将pairlists作为参数。通常,列表用于将参数传递给晶格图形,这是ggplot和后代出现之前的主要高级图形环境。列表还用于传递参数以控制许多函数中的参数。在这种情况下,您可以期望x和y长度相同,因此它可以是数据帧,但没有特别的理由要求这样的结构,并且基本功能点和线将接受命名列表并正确分配它们到他们的论点清单。

( I do not think a matrix could get properly distributed to the x and y arguments of the base plotting routines even of there were row or column names that matched. Matrices are really folded vectors.)

(我不认为矩阵可以正确地分配到基本绘图例程的x和y参数,即使有匹配的行或列名称。矩阵实际上是折叠向量。)

So I think the answer is that passing lists is most consistent with how functional programming is implemented in R.

所以我认为答案是传递列表与函数编程在R中的实现方式最为一致。