问题
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