Skip to content
Prev 17062 / 21318 Next

[Bioc-devel] sapply and vapply

Check the result of sapply(logical(0), all) !

?On 8/12/20, 12:33 PM, "Bioc-devel on behalf of Laurent Gatto" <bioc-devel-bounces at r-project.org on behalf of laurent.gatto at uclouvain.be> wrote:

    Dear all, 

    I have a quick question regarding the usage of vapply and sapply. The former is recommended to insure that the output is always a vector of a specific type. For example:

    > df1 <- data.frame(x = 1:3, y = LETTERS[1:3])     ## OK test
    > df2 <- data.frame(x = 1:3, y = Sys.time() + 1:3) ## Not OK test
    > sapply(df1, class) ## vector of chars, OK
              x           y 
      "integer" "character" 
    > sapply(df2, class) ## ouch, not a vector
    $x
    [1] "integer"

    $y
    [1] "POSIXct" "POSIXt" 

    > vapply(df2, class, character(1)) ## prefer an error rather than a list
    Error in vapply(df2, class, character(1)) : values must be length 1,
     but FUN(X[[2]]) result is length 2

    There are cases, however, were FUN ensures that the output will be of length 1 and of a expected type. For example

    - sapply(X, all) - all() always returns logical(1) 
    - sapply(X, length) - always numeric(1) (integer(1) or double(1) for vectors of more than 2^31 - 1 elements)

    or more generally

    - sapply(X, slot, "myslot") - slot() will always return a character(1) because @myslot is always character(1) (as defined by the class)

    Would you still recommend to use vapply() in such cases?

    Thank you in advance.

    Laurent




    _______________________________________________
    Bioc-devel at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/bioc-devel
Message-ID: <BL0PR04MB66093B73AA39E737BBDEAD61F9420@BL0PR04MB6609.namprd04.prod.outlook.com>
In-Reply-To: <AM0PR03MB6195556A00BE40CB71BEA28591420@AM0PR03MB6195.eurprd03.prod.outlook.com>