【问题标题】:tibble add_row not tidy?tibble add_row 不整洁?
【发布时间】:2018-01-16 08:27:41
【问题描述】:

为什么下面的代码不起作用。 我想在之前从 tibble 复制的 tibble 中添加一行。

 library(dplyr)
 library(tibble)
 tiris <- as_tibble(iris)
 new_row <- tiris %>% tail(1)
 tiris <- tiris %>% add_row(new_row)

错误:列 new_row 必须是一维原子向量或列表

【问题讨论】:

    标签: r row add tibble


    【解决方案1】:

    您创建的new_row 是一行tibble,所以我认为您需要的是 包中的bind_rows 函数,它可以按行组合两个tibbledata frame

    library(dplyr)
    library(tibble)
    tiris <- as_tibble(iris)
    new_row <- tiris %>% tail(1)
    # Combine tiris and new_row
    tiris <- tiris %>% bind_rows(new_row)
    

    查看tiris 的最后两行,它们是相同的。所以我认为bind_rows 有效。

    # View the results
    tail(tiris, 2)
    # # A tibble: 2 x 5
    #   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
    #          <dbl>       <dbl>        <dbl>       <dbl> <fct>    
    # 1         5.90        3.00         5.10        1.80 virginica
    # 2         5.90        3.00         5.10        1.80 virginica
    

    【讨论】:

      猜你喜欢
      • 2020-05-04
      • 2021-07-13
      • 2018-09-13
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多