3

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  
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 2
    I don't get the error when using `SIMPLIFY=F`. – Quinten May 26 '22 at 08:22
  • 3
    Have you maybe got an `F` defined somewhere TarJae? What happens when you type `F` into the console? – Allan Cameron May 26 '22 at 08:24
  • 1
    Nor do I, even with `F <- "zzz"; mapply(c, l, a, SIMPLIFY=F)`, although the output is different. – Limey May 26 '22 at 08:24
  • 4
    @Limey but you will get the error reproduced if you do `F <- function() 1; mapply(c, l, a, SIMPLIFY=F)` – Allan Cameron May 26 '22 at 08:30
  • @tarjae, what is the result of `dput(F)`? – Waldi May 26 '22 at 08:40
  • 1
    @AllanCameron Indeed, I do. I tried with `F` as a string and a data frame, but not as a function. – Limey May 26 '22 at 08:42
  • Sorry had to leave immediately my pc. So `dput(F)` gives: `function (x, n = 1, ...) UseMethod("F")`. – TarJae May 26 '22 at 09:12
  • And after cleaning my workspace and restarting R, the error does not occur!!! – TarJae May 26 '22 at 09:14
  • 1
    @TarJae I wonder which package has a generic function called `F` defined? That seems to me to be asking for trouble! – Allan Cameron May 26 '22 at 09:25
  • @Allan Cameron: I had a look in the environment before cleaning the workspace. Interstingly I did not find a function called `F`. But with your explanation now it is clear why the error occurs. Unfortunately I don't know where this function `F` came from. – TarJae May 26 '22 at 09:30
  • 1
    @TarJae probably you loaded `library(collapse); there is `F` function there. I can reproduce your error `> mapply(c, l, a, SIMPLIFY=F)# Error in SIMPLIFY == "array" : comparison (1) is possible only for atomic and list types` – akrun May 26 '22 at 15:30

2 Answers2

4

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.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

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
akrun
  • 874,273
  • 37
  • 540
  • 662