gpt4 book ai didi

r - 数据框可以做什么而 tibble 不能?

转载 作者:行者123 更新时间:2023-12-03 17:18:53 25 4
gpt4 key购买 nike

Tidyverse 的粉丝经常给出使用小标题而不是数据框的几个优点。它们中的大多数似乎旨在保护用户免于犯错误。例如,与数据框不同,小标题:

  • 不需要 ,drop=FALSE不从数据中删除维度的论据。
  • 不会让$运算符对列名进行部分匹配。
  • 仅当输入向量的长度正好为 1 时才回收它们。

  • 我逐渐相信用小标题替换我所有的数据框。这样做的主要缺点是什么?更具体地说,数据框可以做什么而 tibble 不能?
    先声明一下,我不是在问 data.table或对 Tidyverse 的任何大局反对。我严格询问小标题和数据框。

    最佳答案

    来自 the trouble with tibbles , 你可以阅读 :

    there isn’t really any trouble with tibbles


    然而,

    Some older packages don’t work with tibbles because of their alternative subsetting method. They expect tib[,1]to return a vector, when in fact it will now return another tibble.


    这是@Henrik 在评论中指出的。
    例如, length函数不会返回相同的结果:
    library(tibble)
    tibblecars <- as_tibble(mtcars)
    tibblecars[,"cyl"]
    #> # A tibble: 32 x 1
    #> cyl
    #> <dbl>
    #> 1 6
    #> 2 6
    #> 3 4
    #> 4 6
    #> 5 8
    #> 6 6
    #> 7 8
    #> 8 4
    #> 9 4
    #> 10 6
    #> # ... with 22 more rows
    length(tibblecars[,"cyl"])
    #> [1] 1
    mtcars[,"cyl"]
    #> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
    length(mtcars[,"cyl"])
    #> [1] 32
    其他例子:
  • base::reshape not working with tibbles

  • Invariants for subsetting and subassignment解释行为来自 tibble不同于 data.frame .
    这些限制是已知的,Hadley 在 interacting with legacy code 中给出的解决方案是:

    A handful of functions don’t work with tibbles because they expect df[, 1] to return a vector, not a data frame. If you encounter one of these functions, use as.data.frame() to turn a tibble back to a data frame:

    关于r - 数据框可以做什么而 tibble 不能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66466656/

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