I have a vector with rownames
, so it can be considered a "matrix" with 2 columns (one for filename, one for Topic
):
> res
Topic
jardine-1.docx.md 1
jardine-2.docx.md 1
jardine-a1.docx.md 1
jardine-a2.docx.md 1
jardine-a3.docx.md 1
jardine-a4.docx.md 3
jardine-a5.docx.md 1
jardine-a6.docx.md 3
jardine-a7.docx.md 3
jardine-a8.docx.md 1
...
These are results from the awesome R package on topic modelling, aptly called topicmodels
.
I want to cast
this "vector" into wide format, just for presentation purposes.
This of courses break "tidy data" principles, where "each observation, or case, is in its own row" (see Data Transformation with dplyr
, available here.) Nevertheless, the wide format is much neater than the long format:
Topic1 Topic2 Topic3
1 jardine-1.docx.md jk-1.docx.md jardine-a4.docx.md
2 jardine-2.docx.md jk-2.docx.md jardine-a6.docx.md
3 jardine-a1.docx.md jk-4.docx.md jardine-a7.docx.md
4 jardine-a2.docx.md jk-5.docx.md singtel-1.docx.md
5 jardine-a3.docx.md jk-6.docx.md singtel-2.docx.md
6 jardine-a5.docx.md <NA> singtel-3.docx.md
7 jardine-a8.docx.md <NA> singtel-4.docx.md
8 jk-3.docx.md <NA> singtel-5.docx.md
9 jk-7.docx.md <NA> <NA>
This of course can be done in a variety of ways - one of which looks like this (warning: ugly)
# via cbind
T1=rownames(subset(res, Topic==1))
T2=rownames(subset(res, Topic==2))
T3=rownames(subset(res, Topic==3))
n=max(length(T1),length(T2),length(T3))
length(T1) <- n
length(T2) <- n
length(T3) <- n
cbind(T1,T2,T3)
My question:
Are there any other better ways of presenting this, considering that all code will be within a R Markdown file for presentation purposes?
I would create an interactive table in markdown with the DT package. Link to vignette
library(DT)
datatable(
dataframe, class = 'cell-border stripe', extensions = c('Buttons', 'FixedColumns'), options = list(
dom = 'Bfrtip', scrollX = TRUE, fixedColumns = TRUE,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
)
)
Explore the vignette, it has a bunch of options such as: formating fields with colors and shapes, enabling user to add or remove columns interactivly, scorling through wide tables, ect.