gpt4 book ai didi

r - "dims [product xx] do not match the length of object [xx]"使用 R 函数时出错 `outer`

转载 作者:行者123 更新时间:2023-12-05 00:59:49 29 4
gpt4 key购买 nike

x <- 1:9
names(x) <- paste0("x",x)
y <- 2:5
names(y) <- paste0("y",y)

fun1 <-function(a, b) {paste(class(a),b, sep = "**")} #works
funError <-function(a, b) {paste(class(a),class(b), sep = "**")} #does not work with outer
funNoError<-function(a, b) {paste(a,class(a),class(b),b, sep = "**")} #works with outer

funError(1,2) #is a valid function
outer(x, y, "funError") # fails
outer(x, y, "funNoError") # works

Q1:为什么 outer(x, y, "funError") 不起作用?

Error in dim(robj) <- c(dX, dY) : dims [product 36] do not match the length of object [1]

Q2:为什么 outer(x, y, "funNoError") 有效?它非常相似。

  • 我能看到的唯一区别是 funError 的每个“结果”都是相同的 ("numeric**numeric")。

  • 如果始终具有相同的值是问题所在:为什么这在这里有效?

outer(rep(0,7), 1:10, "^")


好的,我明白了:

lol  <- function(a,b) {"lol"}
lol_v<- Vectorize(lol)

outer(x, y, "lol") # fails with same Error
outer(x, y, "lol_v") # works as expected

最佳答案

xy 都是向量时,我经常解释 outer(x, y, FUN):

xx <- rep(x, times = length(y))
yy <- rep(y, each = length(x))
zz <- FUN(xx, yy)
stopifnot(length(zz) == length(x) * length(y)) ## length = product?
z <- matrix(zz, length(x), length(y))

funError 失败是因为 zz 的长度为 1,而 funNoError 不会因为粘贴 时应用了“回收规则” a(长度大于 1 的向量)和 class(a)(长度为 1 的向量)。

这是说明性的,因为您将看到为什么 outer(1:5, 1:5, "+") 有效,但 outer(1:5, 1:5, sum) 失败。基本上,FUN 必须能够处理 xxyy element-wise。否则,使用名为 Vectorize 的糖函数包装 FUN。更多细节稍后给出。

请注意,“列表”也是向量的有效模式。所以 outer 可以用于一些非标准的东西,比如 How to perform pairwise operation like `%in%` and set operations for a list of vectors .


您也可以将矩阵/数组传递给 outer。鉴于它们只是带有“dim”属性的向量(可选地带有“dimnames”),outer 的工作方式不会改变。

x <- matrix(1:4, 2, 2)  ## has "dim"
y <- matrix(1:9, 3, 3) ## has "dim"

xx <- rep(x, times = length(y)) ## xx <- rep(c(x), times = length(y))
yy <- rep(y, each = length(x)) ## yy <- rep(c(y), each = length(x))
zz <- "*"(xx, yy)
stopifnot(length(zz) == length(x) * length(y)) ## length = product?
z <- "dim<-"( zz, c(dim(x), dim(y)) )

z0 <- outer(x, y, "*")
all.equal(z, z0)
#[1] TRUE

?outer 用通俗易懂的语言解释了上面的代码。

 ‘X’ and ‘Y’ must be suitable arguments for ‘FUN’.  Each will be
extended by ‘rep’ to length the products of the lengths of ‘X’ and
‘Y’ before ‘FUN’ is called.

‘FUN’ is called with these two extended vectors as arguments (plus
any arguments in ‘...’). It must be a vectorized function (or the
name of one) expecting at least two arguments and returning a
value with the same length as the first (and the second).

Where they exist, the [dim]names of ‘X’ and ‘Y’ will be copied to
the answer, and a dimension assigned which is the concatenation of
the dimensions of ‘X’ and ‘Y’ (or lengths if dimensions do not
exist).

“矢量化”这个词不是 the most discussed one in R on performance .它的意思是“向量化函数的 Action ”:

## for FUN with a single argument
FUN( c(x1, x2, x3, x4) ) = c( FUN(x1), FUN(x2), FUN(x3), FUN(x4) )

## for FUN with two arguments
FUN( c(x1, x2, x3, x4), c(y1, y2, y3, y4) )
= c( FUN(x1, y1), FUN(x2, y2), FUN(x3, y3), FUN(x4, y4) )

有些函数说 "+""*"paste 的行为是这样的,但许多其他函数却不是这样,比如 sumprod。 R 中的 *apply 系列函数可以帮助您将函数 Action 矢量化,或者您可以编写自己的循环来实现相同的效果。


另一个值得一读的优质问答:Why doesn't outer work the way I think it should (in R)?

关于r - "dims [product xx] do not match the length of object [xx]"使用 R 函数时出错 `outer`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52317124/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com