简体   繁体   English

如何在R中的数据框中合并列标题

[英]How to merge Column headers in dataframe in r

I have a data-frame in R that looks like this. 我在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)

I'm trying to merge the column headers - Housing$Housing & Housing$Values into a Single cell called Housing_characteristics and the rest of the data frame must be the same. 我正在尝试将列标题Housing_characteristics Housing$HousingHousing$Values合并到一个名为Housing_characteristics的单个单元格中,其余数据框必须相同。 Exactly like an Excel spreadsheet. 就像Excel电子表格一样。

https://i.stack.imgur.com/J7AjF.jpg https://i.stack.imgur.com/J7AjF.jpg

I want the output to look something like this - https://i.stack.imgur.com/qHd0C.jpg 我希望输出看起来像这样-https://i.stack.imgur.com/qHd0C.jpg

I have a bunch of these data-frames that I'm displaying in an RShiny application and need to format the headers for a clean look. 我有一堆要在RShiny应用程序中显示的数据帧,并且需要格式化标题以使外观整洁。

Thanks! 谢谢!

This might be the closest thing you can get: 这可能是您可以获得的最接近的东西:

library(stargazer)
stargazer(Housing, summary = FALSE, type = "text",
          rownames = FALSE, align = TRUE, title = "Housing Characteristics")

Text Table: 文字表:

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% 
-----------------------------------

Note that this is not a data.frame . 请注意,这不是 data.frame This is a table in text form outputted into your console, so you cannot manipulate it like a data.frame . 这是输出到控制台的文本形式的表,因此您不能像data.frame一样操作它。

If you are fine with latex tables in your shiny app, you can also use the type = latex option, which is the default: 如果您对闪亮的应用程序中的乳胶表满意,还可以使用type = latex选项,这是默认选项:

library(stargazer)
stargazer(Housing, summary = FALSE, header = FALSE,
          title = "Housing Characteristics")

Latex Code: 乳胶代码:

\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}

在此处输入图片说明

This would look quite nice on a shiny app! 在闪亮的应用程序上看起来会很好!

Modify the displayed output and not the data.frame . 修改显示的输出,而不是data.frame The following will draw an interactive table with title and still allow columns to be sorted: 下面将绘制一个带有标题的交互式表格,并且仍然允许对列进行排序:

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)

在此处输入图片说明

example derived from section 2.5 of http://rstudio.github.io/DT/ 示例源自http://rstudio.github.io/DT/的 2.5节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM