从R中的数据框创建交叉表格

从R中的数据框创建交叉表格,r,dataframe,tabular,R,Dataframe,Tabular,我有这样一个数据框: df <- data.frame(Country = rep(c("US","CA"),each=3), Variable = c("Inflation","Unemployment","Interest rate"), Month = rnorm(6), Quarter = rnorm(6)+2, Year=rnorm(6)+3) df您不能像在excel中那样合并数据。在R中框出单元格,但您可以

我有这样一个数据框:

df <- data.frame(Country = rep(c("US","CA"),each=3),
                 Variable = c("Inflation","Unemployment","Interest rate"),
                 Month = rnorm(6), Quarter = rnorm(6)+2, Year=rnorm(6)+3)

df您不能像在excel中那样合并
数据。在R中框出
单元格,但您可以组合
国家
和期间列。下面是一个使用
dplyr
+
tidyr
的解决方案:

library(dplyr)
library(tidyr)

df %>%
  gather(var, value, Month:Year) %>%
  unite("var", Country, var) %>%
  spread(var, value)
结果:

       Variable   CA_Month CA_Quarter  CA_Year   US_Month US_Quarter  US_Year
1     Inflation  0.2760235   1.758310 4.233976 -0.4321298  3.6232025 5.149919
2 Interest rate -0.5208693   1.227022 3.412022  1.2283928  3.6858872 3.495870
3  Unemployment -1.0489755   1.531800 3.634362  1.6898725  0.9299318 1.665646
               CA                    US                   
 Variable      Month   Quarter Year  Month   Quarter Year 
 Inflation      0.5269 2.152   3.854 -0.9456 3.764   1.432
 Interest rate  1.3974 1.820   3.340  0.4520 1.734   3.962
 Unemployment  -0.2303 3.377   3.419 -0.6652 2.486   2.739
\begin{tabular}{lcccccc}
\hline
 & \multicolumn{6}{c}{Country} \\ 
 & \multicolumn{3}{c}{CA} & \multicolumn{3}{c}{US} \\ 
Variable  & Month & Quarter & Year & Month & Quarter & \multicolumn{1}{c}{Year} \\ 
\hline
Inflation  & $\phantom{-}0.5269$ & $2.152$ & $3.854$ & $-0.9456$ & $3.764$ & $1.432$ \\
Interest rate  & $\phantom{-}1.3974$ & $1.820$ & $3.340$ & $\phantom{-}0.4520$ & $1.734$ & $3.962$ \\
Unemployment  & $-0.2303$ & $3.377$ & $3.419$ & $-0.6652$ & $2.486$ & $2.739$ \\
\hline 
\end{tabular}
要实际创建一个合并了
Country
的表(用于报告),下面介绍如何使用
tables
包使用一行程序执行此操作:

library(tables)

tabular(Variable ~  Heading()*Country*Heading()*identity*(Month + Quarter + Year), data=df)
结果:

       Variable   CA_Month CA_Quarter  CA_Year   US_Month US_Quarter  US_Year
1     Inflation  0.2760235   1.758310 4.233976 -0.4321298  3.6232025 5.149919
2 Interest rate -0.5208693   1.227022 3.412022  1.2283928  3.6858872 3.495870
3  Unemployment -1.0489755   1.531800 3.634362  1.6898725  0.9299318 1.665646
               CA                    US                   
 Variable      Month   Quarter Year  Month   Quarter Year 
 Inflation      0.5269 2.152   3.854 -0.9456 3.764   1.432
 Interest rate  1.3974 1.820   3.340  0.4520 1.734   3.962
 Unemployment  -0.2303 3.377   3.419 -0.6652 2.486   2.739
\begin{tabular}{lcccccc}
\hline
 & \multicolumn{6}{c}{Country} \\ 
 & \multicolumn{3}{c}{CA} & \multicolumn{3}{c}{US} \\ 
Variable  & Month & Quarter & Year & Month & Quarter & \multicolumn{1}{c}{Year} \\ 
\hline
Inflation  & $\phantom{-}0.5269$ & $2.152$ & $3.854$ & $-0.9456$ & $3.764$ & $1.432$ \\
Interest rate  & $\phantom{-}1.3974$ & $1.820$ & $3.340$ & $\phantom{-}0.4520$ & $1.734$ & $3.962$ \\
Unemployment  & $-0.2303$ & $3.377$ & $3.419$ & $-0.6652$ & $2.486$ & $2.739$ \\
\hline 
\end{tabular}
tabular
使用其唯一的表达式生成表格格式:

  • ~
    将行和列的表达式分开。我正在显示
    变量的行

  • *
    表示将一列嵌套到另一列中。在本例中,我将
    Month:Year
    列嵌套在
    Country

  • identity
    指定在每个单元格中显示实际值

  • 标题
    用字符串替换下一项的标题。在本例中,我将用空白替换
    “国家”
    “身份”

要输出为latex,可以使用
latex
函数包装整个表达式:

latex(tabular(Variable ~  Heading()*Country*Heading()*identity*(Month + Quarter + Year), data=df))
结果:

       Variable   CA_Month CA_Quarter  CA_Year   US_Month US_Quarter  US_Year
1     Inflation  0.2760235   1.758310 4.233976 -0.4321298  3.6232025 5.149919
2 Interest rate -0.5208693   1.227022 3.412022  1.2283928  3.6858872 3.495870
3  Unemployment -1.0489755   1.531800 3.634362  1.6898725  0.9299318 1.665646
               CA                    US                   
 Variable      Month   Quarter Year  Month   Quarter Year 
 Inflation      0.5269 2.152   3.854 -0.9456 3.764   1.432
 Interest rate  1.3974 1.820   3.340  0.4520 1.734   3.962
 Unemployment  -0.2303 3.377   3.419 -0.6652 2.486   2.739
\begin{tabular}{lcccccc}
\hline
 & \multicolumn{6}{c}{Country} \\ 
 & \multicolumn{3}{c}{CA} & \multicolumn{3}{c}{US} \\ 
Variable  & Month & Quarter & Year & Month & Quarter & \multicolumn{1}{c}{Year} \\ 
\hline
Inflation  & $\phantom{-}0.5269$ & $2.152$ & $3.854$ & $-0.9456$ & $3.764$ & $1.432$ \\
Interest rate  & $\phantom{-}1.3974$ & $1.820$ & $3.340$ & $\phantom{-}0.4520$ & $1.734$ & $3.962$ \\
Unemployment  & $-0.2303$ & $3.377$ & $3.419$ & $-0.6652$ & $2.486$ & $2.739$ \\
\hline 
\end{tabular}

在使用
数据进行重塑后,我们可以尝试使用
knitr
中的
kable
。table

library(data.table)
library(knitr)
library(kableExtra)

dt <- dcast(setDT(df),  Variable ~ Country, value.var = c('Month', 'Quarter', 'Year'))
nm1 <- names(dt)
nm2 <- c(" ", unique(sub(".*_", "", nm1)[-1]))

setnames(dt, sub("_.*", "", nm1))
setcolorder(dt, order(ave(seq_along(dt), names(dt), FUN = seq_along)))

kable(dt, 'html') %>%  
   kable_styling('striped') %>% 
    add_header_above(c(' ' = 1, 'CA' = 3, 'US' = 3))
库(data.table)
图书馆(knitr)
图书馆(kableExtra)

dt可能
库(data.table);dcast(setDT(df),Variable~Country,value.var=c('Month','Quarter','Year'))
非常感谢您的帮助。但是,我需要的data.frame或table与图中所示完全相同。我看到tables包为汇总统计数据做了类似的操作,但我的数据不能得到相同的结果。@RenatoLeripio是否可以生成此表取决于您是否希望输出可操作。您想要一个
data.frame
作为输出表,还是只想要一个文本形式的表,您可以在其中复制和粘贴?实际上,我需要该表作为报告文档。因此,输出很可能是LaTeX或文本形式。@RenatoLeripio您可能应该在您的问题中加入此细节。有关如何使用
tables
包创建表的信息,请参见我的更新。是的,你说得对。我已经更新了这个问题。我感谢你的帮助。非常感谢。