使用外,以产生列表的阵列 [英] Using outer to produce an array of lists
问题描述
我跑外
上返回一个列表,我期待它返回列表的数组,因为文件说,这样一个功能:
I'm running outer
on a function that returns a list, and I'm expecting it to return an array of lists, since the documentation says so:
阵列X Y的和外产物是用维数组A
C(变暗(X),暗(Y)),其中元素A [C(arrayindex.x,arrayindex.y)] =
FUN(X [arrayindex.x],Y [arrayindex.y],...)。
The outer product of the arrays X and Y is the array A with dimension c(dim(X), dim(Y)) where element A[c(arrayindex.x, arrayindex.y)] = FUN(X[arrayindex.x], Y[arrayindex.y], ...).
但是,下面的code抛出一个错误:
However, the following code throws an error:
outer(1:5, 1:5, Vectorize(function(x, y) list(x=x, y=y)))
#Error in outer(1:5, 1:5, Vectorize(function(x, y) list(x = x, y = y))) :
# dims [product 25] do not match the length of object [50]
似乎外
正在试图打平我的名单,而我希望他们是完整的。似乎有控制这个无参数(不像简化
为 sapply
)。你会建议什么?我唯一的要求是保持至少两个维度为索引的目的下来就行了。
It seems that outer
is trying to flatten my lists, whereas I'd like them to be intact. There seems to be no parameter for controlling this (unlike simplify
for sapply
). What would you propose? My only requirement is to keep at least two dimensions for indexing purposes down the line.
推荐答案
您可以尝试
outer(1:5, 1:5, Vectorize(function(x,y) list(c(x=x, y=y))))
该输出25种元素,每个是一个名为向量列表
which output a list of 25 elements with each being a named vector
或另一种选择是将这个包裹在一个'清单'来创建列表清单。
or another option would be to wrap this in a 'list' to create a list of lists.
outer(1:5, 1:5, Vectorize(function(x,y) list(list(x=x, y=y))))
这篇关于使用外,以产生列表的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!