Reputation: 43
I have a data frame like this:
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)
and I would like to transform it into something like this:
I have tried the tables package, but did not succeed. Does anyone have any clue about how to achieve this?
EDIT: The output may be either in LaTeX or Text form.
Upvotes: 4
Views: 1445
Reputation: 18691
You can't merge data.frame
cells in R like you would in excel, but you can combine the Country
and period columns. Here's a solution with dplyr
+ tidyr
:
library(dplyr)
library(tidyr)
df %>%
gather(var, value, Month:Year) %>%
unite("var", Country, var) %>%
spread(var, value)
Result:
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
To actually create a table (for reporting) that has the Country
merged, here is how to do it with a one-liner using the tables
package:
library(tables)
tabular(Variable ~ Heading()*Country*Heading()*identity*(Month + Quarter + Year), data=df)
Result:
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
tabular
uses its unique expression to generate the table formats:
~
separates the expression for rows from columns. I am displaying the rows of Variable
.
*
means that you are nesting one column into another. In this case, I am nesting the Month:Year
columns in Country
.
identity
specifies that you are displaying the actual values in each cell.
Heading
replaces the heading of the next item with a string. In this case, I am replacing "Country"
and "identity"
with blank.
To output as latex, you can wrap the entire expression with the latex
function:
latex(tabular(Variable ~ Heading()*Country*Heading()*identity*(Month + Quarter + Year), data=df))
Result:
\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}
Upvotes: 0
Reputation: 887901
We could try with kable
from knitr
after reshaping with data.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))
<table class="table table-striped" style="margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="border-bottom:hidden" colspan="1"></th>
<th style="text-align:center; border-bottom:hidden; padding-bottom:0; padding-left:3px;padding-right:3px;" colspan="3"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px;">CA</div></th>
<th style="text-align:center; border-bottom:hidden; padding-bottom:0; padding-left:3px;padding-right:3px;" colspan="3"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px;">US</div></th>
</tr>
<tr>
<th style="text-align:left;"> Variable </th>
<th style="text-align:right;"> Month </th>
<th style="text-align:right;"> Quarter </th>
<th style="text-align:right;"> Year </th>
<th style="text-align:right;"> Month </th>
<th style="text-align:right;"> Quarter </th>
<th style="text-align:right;"> Year </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> Inflation </td>
<td style="text-align:right;"> -0.5836272 </td>
<td style="text-align:right;"> 2.0023119 </td>
<td style="text-align:right;"> 2.530939 </td>
<td style="text-align:right;"> -0.5458808 </td>
<td style="text-align:right;"> 2.444585 </td>
<td style="text-align:right;"> 2.237786 </td>
</tr>
<tr>
<td style="text-align:left;"> Interest rate </td>
<td style="text-align:right;"> 0.2660220 </td>
<td style="text-align:right;"> 2.5982691 </td>
<td style="text-align:right;"> 4.536252 </td>
<td style="text-align:right;"> 0.4196231 </td>
<td style="text-align:right;"> 1.151630 </td>
<td style="text-align:right;"> 3.332244 </td>
</tr>
<tr>
<td style="text-align:left;"> Unemployment </td>
<td style="text-align:right;"> 0.8474600 </td>
<td style="text-align:right;"> 0.6830919 </td>
<td style="text-align:right;"> 2.665013 </td>
<td style="text-align:right;"> 0.5365853 </td>
<td style="text-align:right;"> 1.533505 </td>
<td style="text-align:right;"> 1.570910 </td>
</tr>
</tbody>
Note that replacing the 'html'
with 'latex'
creates the latex output
kable(dt, 'latex') %>%
kable_styling('striped') %>%
add_header_above(c(' ' = 1, 'CA' = 3, 'US' = 3))
-latex output
\begin{table}[H]
\centering
\begin{tabular}{l|r|r|r|r|r|r}
\hline
\multicolumn{1}{c|}{ } & \multicolumn{3}{|c|}{CA} & \multicolumn{3}{|c}{US} \\
\cline{2-4} \cline{5-7}
Variable & Month & Quarter & Year & Month & Quarter & Year\\
\hline
Inflation & -0.5836272 & 2.0023119 & 2.530939 & -0.5458808 & 2.444585 & 2.237786\\
\hline
Interest rate & 0.2660220 & 2.5982691 & 4.536252 & 0.4196231 & 1.151630 & 3.332244\\
\hline
Unemployment & 0.8474600 & 0.6830919 & 2.665013 & 0.5365853 & 1.533505 & 1.570910\\
\hline
\end{tabular}
\end{table}
Upvotes: 3