false vs. f:错误mapply' s简化参数:在简化= f中

发布于 2025-02-01 14:09:54 字数 1395 浏览 1 评论 0原文

这个问题是指此有没有最好的方法将列表值附加到r?中的列表的sublist。

解决方案之一是:

a <- list(3,5,7)
l <- list(c(1,2,3), c(2,1,4), c(4,7,6))

mapply(c, l, a, SIMPLIFY=F)

如果我尝试将其应用于我的计算机上,则会发现错误:

Error in SIMPLIFY == "array" : 
  comparison (1) is possible only for atomic and list types

如果使用此错误 - &gt;没有错误:

mapply(c, l, a, SIMPLIFY = FALSE)

我想了解为什么在使用Simplify = F而不是在Simplify = false中发生错误。

我正在使用rstudio -cloud:

> version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.3                         
year           2022                        
month          03                          
day            10                          
svn rev        81868                       
language       R                           
version.string R version 4.1.3 (2022-03-10)
nickname       One Push-Up  

This Question refers to this Is there a best way to append a list values to a sublist of a list in R?.

One of the solution is this:

a <- list(3,5,7)
l <- list(c(1,2,3), c(2,1,4), c(4,7,6))

mapply(c, l, a, SIMPLIFY=F)

If I try to apply it on my machine I get the error:

Error in SIMPLIFY == "array" : 
  comparison (1) is possible only for atomic and list types

If I use this -> there is no error:

mapply(c, l, a, SIMPLIFY = FALSE)

I want to understand why the error occurs in using SIMPLIFY =F and not in SIMPLIFY = FALSE.

I am using rstudio - cloud:

> version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.3                         
year           2022                        
month          03                          
day            10                          
svn rev        81868                       
language       R                           
version.string R version 4.1.3 (2022-03-10)
nickname       One Push-Up  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2025-02-08 14:09:54

在全新的r会话中,此错误不可重复:

a <- list(3,5,7)
l <- list(c(1,2,3), c(2,1,4), c(4,7,6))

mapply(c, l, a, SIMPLIFY=F)
#> [[1]]
#> [1] 1 2 3 3
#> 
#> [[2]]
#> [1] 2 1 4 5
#> 
#> [[3]]
#> [1] 4 7 6 7

但是,我们可以通过函数覆盖f来复制您的错误:

F <- function() {}; mapply(c, l, a, SIMPLIFY=F)
#> Error in SIMPLIFY == "array" : 
#>   comparison (1) is possible only for atomic and list types

这表明您在搜索路径上的某个位置,称为> f

我们可以猜到它是一个函数而不是数据框架或向量的原因,是因为只有将某些内容传递给Simplefify没有一个没有一个没有的错误 则函数是最有可能的候选者(尤其是因为它称为f

==运算符定义的方法,并且如果您查看mapply的源代码,,您会看到Simplify参数将传递到Simplefify2Array时,并且仅当它未对Atomic false false

function (FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE) 
{
    FUN <- match.fun(FUN)
    dots <- list(...)
    answer <- .Internal(mapply(FUN, dots, MoreArgs))
    if (USE.NAMES && length(dots)) {
        if (is.null(names1 <- names(dots[[1L]])) && is.character(dots[[1L]])) 
            names(answer) <- dots[[1L]]
        else if (!is.null(names1)) 
            names(answer) <- names1
    }
    if (!isFALSE(SIMPLIFY)) 
        simplify2array(answer, higher = (SIMPLIFY == "array"))
    else answer
}

如果它不是原子false,则使用==进行测试,对简化参数进行等于字符串“ array”操作员。如果无法用字符串测试对象的平等,我们将获得此错误。

我认为这个问题是一个很好的例子,说明为什么永远不要将f用作R中的变量名称,以及为什么人们应该始终使用false而不是f f 。 /代码>。

In a fresh R session, this error isn't reproducible:

a <- list(3,5,7)
l <- list(c(1,2,3), c(2,1,4), c(4,7,6))

mapply(c, l, a, SIMPLIFY=F)
#> [[1]]
#> [1] 1 2 3 3
#> 
#> [[2]]
#> [1] 2 1 4 5
#> 
#> [[3]]
#> [1] 4 7 6 7

However, we can replicate your error by overwriting F with a function:

F <- function() {}; mapply(c, l, a, SIMPLIFY=F)
#> Error in SIMPLIFY == "array" : 
#>   comparison (1) is possible only for atomic and list types

Which suggests that you have a function somewhere on your search path called F.

The reason that we can guess it is a function rather than, say, a data frame or a vector, is that we would only get this error if there is something being passed to SIMPLIFY that does not have a method defined for the == operator, and a function is the most likely candidate (especially since it is called F)

If you look at the source code for mapply, you will see that the SIMPLIFY argument gets passed to simplify2array if and only if it does not evaluate to an atomic FALSE:

function (FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE) 
{
    FUN <- match.fun(FUN)
    dots <- list(...)
    answer <- .Internal(mapply(FUN, dots, MoreArgs))
    if (USE.NAMES && length(dots)) {
        if (is.null(names1 <- names(dots[[1L]])) && is.character(dots[[1L]])) 
            names(answer) <- dots[[1L]]
        else if (!is.null(names1)) 
            names(answer) <- names1
    }
    if (!isFALSE(SIMPLIFY)) 
        simplify2array(answer, higher = (SIMPLIFY == "array"))
    else answer
}

If it is not an atomic FALSE, the SIMPLIFY argument is then tested for equality to the string "array" using the == operator. If the object cannot be tested for equality with a character string, we will get this error.

I think this question is a great example of why one should never use F as a variable name in R, and why one should always use FALSE instead of F.

黎夕旧梦 2025-02-08 14:09:54

正如OP提到的那样,它们在控制台中没有创建任何功能f,它可能来自加载collapse package

library(collapse)

,然后我们检查?f

flag是计算滞后和铅的S3通用。 lf是代表滞后和铅操作器的标志的包装器...

因此,可以重现该错误

> mapply(c, l, a, SIMPLIFY=F)
Error in SIMPLIFY == "array" : 
  comparison (1) is possible only for atomic and list types

As the OP mentioned that they didn't create any function F in the console, it could be coming from loading collapse package

library(collapse)

and then we check ?F

flag is an S3 generic to compute (sequences of) lags and leads. L and F are wrappers around flag representing the lag- and lead-operators...

Thus, the error can be reproduced

> mapply(c, l, a, SIMPLIFY=F)
Error in SIMPLIFY == "array" : 
  comparison (1) is possible only for atomic and list types
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文