Reputation: 1445
This question is related to the one here, but I feel that the solutions provided there don't answer the question.
Split xtable ouput into sub tables
My question is nearly the same. In R, I have the following aggregated data table:
> 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
Displaying the data frame in R overflows, so definitely won't fit in a latex document. In my example, I can assume that splitting this into two "subtables" with 8 columns each would fit on my page.
print(xtable(agg2, caption=My awesome caption,
label="tbl:myTable", digits=3))
How can I force my "table" to actually be 2 sub-tables with 8 columns each? In other words, how can I force the output to make n
sub-tables with a maximum of k
columns each? (n
would be decided by the function, not the user)
Note: I do not want my table to be printed sideways. And to prevent over-complicating the Q&A, I'm not considering the possibility that the table will span multiple pages.
Upvotes: 0
Views: 539
Reputation: 7659
This does no error checking, and you have to determine the number of columns manually, but it should give you a starting point. Edited to produce sub-tables, as I understand it.
# 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 )
}
Call the function
aggs <- varTable( agg2, 6 )
and print it altogether with
for( i in 2 : ( aggs[[ 1 ]] + 1 ) ) print( aggs[[ i ]], hline.after = 0 )
This gives me
Upvotes: 1