How to transpose a table in an R markdown document?

↘锁芯ラ 提交于 2021-01-28 12:23:42

问题


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")

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.

Any easy way?


回答1:


How about using the t() function:

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.



来源:https://stackoverflow.com/questions/20848622/how-to-transpose-a-table-in-an-r-markdown-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!