【问题标题】:format tibble in a specific format shown以显示的特定格式格式化小标题
【发布时间】:2021-03-04 15:16:12
【问题描述】:

我有一个输出表作为小标题。下面是例子-

library(tidyverse)

Var <- c("Constant_A", "Constant_B", "Cost_A", "Cost_B")
Param <- c(2, 0, 1, 0)
tstat <- c(5, NA, 4, NA)

tbl <- tibble(Var, Param, t_stat)
  1. 我希望以特定方式格式化它,如下面的截图所示
  2. 如何将表格复制到我的 Word 文档中。

【问题讨论】:

    标签: r tibble


    【解决方案1】:

    在 R 中,从类 "data.frame" 继承的对象,例如 tibbles,只能有一个标头。以下内容会将数据转换为问题中的格式。

    tbl %>%
      separate(Var, c("Var", "What")) %>%
      pivot_wider(
        id_cols = Var,
        names_from = What,
        names_glue = "{What}_{.value}",
        values_from = c(Param, tstat)
      ) %>%
      select(Var, starts_with('A'), starts_with('B'))
    ## A tibble: 2 x 5
    #  Var      A_Param A_tstat B_Param B_tstat
    #  <chr>      <dbl>   <dbl>   <dbl>   <dbl>
    #1 Constant       2       5       0      NA
    #2 Cost           1       4       0      NA
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 2014-12-15
      • 2011-07-12
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 2013-07-29
      相关资源
      最近更新 更多