【问题标题】:R xtable: Wrap overflowing columns into subtablesR xtable:将溢出的列包装到子表中
【发布时间】:2015-09-26 04:55:42
【问题描述】:

此问题与此处的问题有关,但我觉得那里提供的解决方案无法回答问题。

Split xtable ouput into sub tables

我的问题几乎相同。在 R 中,我有以下聚合数据表:

> agg2
       x1.Err     x1.Pct       x2.Err     x2.Pct       x3.Err     x3.Pct      x4.Err     x4.Pct        x5.Err     x5.Pct     x6.Err
1 S.noun_noun 0.13121019  S.noun_noun 0.19791667  S.noun_noun 0.16730769 S.noun_noun 0.17659574  S.noun_noun 0.18352941 S.noun_noun
2 S.verb_verb 0.12611465  S.verb_verb 0.13750000  S.verb_verb 0.14615385 S.verb_verb 0.14042553  S.verb_verb 0.13176471 S.verb_verb
3   S.det_det 0.04076433    S.det_det 0.04375000    S.det_det 0.05384615 S.prep_prep 0.04042553 S.coord_prep 0.04000000 S.prep_prep
4 S.verb_noun 0.03821656    S.prn_prn 0.03958333 S.coord_prep 0.04423077   S.prn_prn 0.03829787    S.det_det 0.04000000   S.det_det
5  S.prep_det 0.03312102 S.coord_prep 0.03750000  S.prep_prep 0.04230769   S.det_det 0.03617021   S.prep_det 0.03764706   S.prn_prn
      x6.Pct      x7.Err     x7.Pct       x8.Err     x8.Pct
1 0.16839378 S.noun_noun 0.18330309  S.noun_noun 0.18281536
2 0.14766839 S.verb_verb 0.14700544  S.verb_verb 0.13893967
3 0.04663212   S.det_det 0.03811252   S.verb_prn 0.03839122
4 0.03886010   S.prn_prn 0.03448276  S.verb_noun 0.03656307
5 0.03108808 S.verb_prep 0.03448276 S.coord_prep 0.03473492

在 R 中显示数据框会溢出,因此绝对不适合乳胶文档。在我的示例中,我可以假设将其拆分为两个“子表”,每个 8 列适合我的页面。

 print(xtable(agg2, caption=My awesome caption, 
             label="tbl:myTable", digits=3))

如何强制我的“表”实际上是 2 个子表,每个子表有 8 列?换句话说,如何强制输出生成 n 子表,每个子表最多有 k 列? (n 将由功能决定,而不是用户)

注意:我不希望我的表格被横向打印。并且为了防止问答过于复杂,我没有考虑表格跨越多个页面的可能性。

【问题讨论】:

    标签: r latex tabular xtable


    【解决方案1】:

    这不会进行错误检查,您必须手动确定列数,但它应该为您提供一个起点。据我了解,已编辑以生成子表。

    # define a function that takes two parameters:
    # - your long data.frame 
    # - the number of columns you want to print in one table
    varTable <- function( agg, cols ) 
    {
      tables <- ceiling( length( agg ) / cols )    # number of tables to produce
      # list, first element is number of sub-tables
      p <- list( tables )
      # produce as many table as needed with the full number of columns
      for( i in 0 : ( tables - 2 ) ) p[[ i + 2 ]] <- xtable( agg[ ( cols * i + 1):( cols * i + cols ) ] )
      # last table may have less columns and takes the caption
      p[[ i + 3 ]] <- xtable( agg[ ( cols * ( i + 1  ) + 1):( length( agg ) ) ], caption = "bla" )
      # return the list with xtable objects that can now be printed one by one
      return( p )
    }
    

    调用函数

    aggs <- varTable( agg2, 6 )
    

    并完全打印出来

    for( i in 2 : ( aggs[[ 1 ]] + 1 ) ) print( aggs[[ i ]], hline.after = 0 )
    

    这给了我

    【讨论】:

    • 非常接近。谢谢!现在我想我必须抑制 xtable 中的一些输出,以尝试让所有内容都流入一个表中。
    • 单表?也许你可以编辑你的问题,以澄清子表如何进入一个表,特别是前一个子表的最后一行如何混合到下一个表的标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2021-05-20
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多