如何转置 R Markdown 文档中的表格? [英] How to transpose a table in an R markdown document?

查看:72
本文介绍了如何转置 R Markdown 文档中的表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我打印了一个名为 summary_table 的数据框,如下所示:

Suppose I print a data frame called summary_table as follows:

summary_table = data.frame(a=c(1,2,3), b=c(11,12,13),c=c(21,22,23),d=c(31,32,33),e=c(41,42,43),f=c(51,52,53),g=c(61,62,63),h=c(71,72,73),i=c(81,82,83),j=c(91,92,93),k=c(101,102,103),l=c(111,112,113))

print(xtable(summary_table,
         align=rep("r",13),
         caption="A summary of stuff."),
  table.placement="H")

现在假设 summary_table 只有三行,但有十二列.我想翻转(转置)表格,使列变成行.

Now suppose summary_table has only three rows, but twelve columns. I'd like to flip (transpose) the table so that the columns become rows.

有什么简单的方法吗?

推荐答案

如何使用 t() 函数:

print(xtable(t(summary_table),
         align=rep("r",4),
         caption="A summary of stuff."),
  table.placement="H")

您的示例无法重现,因此我无法对其进行测试.

Your example is not reproducible, so I cannot test it.

这篇关于如何转置 R Markdown 文档中的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆