Search code examples
rtabulartwo-way

Two-way table with tables producing 1's


I'm trying to make a simple two-way table with tabluar(), but all I'm getting is 1's in the output.

Why are my estimates not showing up and I get 1's instead?

Sample Code

pred.table <- structure(list(temp = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 5L), .Label = c("+1C", "+2C", "+3C", "+4C", 
"+5C"), class = "factor"), crop = structure(c(1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C", 
"D", "E"), class = "factor"), reg = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CS", "DD", "P"
), class = "factor"), impact = c(-4.5, -11.15, -19.62, -29.37, 
-39.8, -7.22, -19.77, -35.53, -51.96, -66.89, 0.47, -0.42, -2.79, 
-6.66, -11.96, -9.02, -19.94, -32.06, -44.58, -56.66, -5.11, 
-12.97, -23.04, -34.53, -46.57, 0.24, 0.28, 0.06, -0.45, -1.27, 
-12.11, -24.4, -37.07, -49.95, -62.45, -11.67, -23.86, -36.03, 
-47.69, -58.41, -4.15, -9.14, -14.91, -21.34, -28.29), se = c(2.39, 
2.41, 2.44, 2.47, 2.5, 4.18, 4.11, 4.01, 3.91, 3.78, 2.13, 2.13, 
2.14, 2.15, 2.16, 0.6, 0.62, 0.65, 0.69, 0.74, 0.82, 0.82, 0.83, 
0.83, 0.83, 0.47, 0.48, 0.5, 0.51, 0.53, 2.47, 2.5, 2.55, 2.62, 
2.72, 14.88, 14.96, 15.08, 15.23, 15.42, 8.3, 8.39, 8.49, 8.6, 
8.74)), class = "data.frame", row.names = c(NA, -45L), .Names = c("temp", 
"crop", "reg", "impact", "se"))

Make Table

library(tables)
tab1 <- latex(tabular(temp ~ crop*reg*(impact + se), data = pred.table))

Output

\begin{tabular}{lcccccccccccccccccccccccccccccc}
\hline
 & \multicolumn{30}{c}{crop} \\ 
 & \multicolumn{6}{c}{A} & \multicolumn{6}{c}{B} & \multicolumn{6}{c}{C} & \multicolumn{6}{c}{D} & \multicolumn{6}{c}{E} \\ 
 & \multicolumn{6}{c}{reg} & \multicolumn{6}{c}{reg} & \multicolumn{6}{c}{reg} & \multicolumn{6}{c}{reg} & \multicolumn{6}{c}{reg} \\ 
 & \multicolumn{2}{c}{CS} & \multicolumn{2}{c}{DD} & \multicolumn{2}{c}{P} & \multicolumn{2}{c}{CS} & \multicolumn{2}{c}{DD} & \multicolumn{2}{c}{P} & \multicolumn{2}{c}{CS} & \multicolumn{2}{c}{DD} & \multicolumn{2}{c}{P} & \multicolumn{2}{c}{CS} & \multicolumn{2}{c}{DD} & \multicolumn{2}{c}{P} & \multicolumn{2}{c}{CS} & \multicolumn{2}{c}{DD} & \multicolumn{2}{c}{P} \\ 
temp  & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & se & impact & \multicolumn{1}{c}{se} \\ 
\hline
+1C  & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ \\
+2C  & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ \\
+3C  & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ \\
+4C  & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ \\
+5C  & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $1$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ \\
\hline 
\end{tabular}

Solution

  • I think your problem lies within this part of the code:

    tabular(temp ~ crop*reg*(impact + se), data = pred.table)
    

    if your read the documentation for ?tabular, you'll find that passage:

    If the term evaluates to a function, it should be a summary function that produces a scalar value when applied to a vector of values, and that scalar will be displayed in the table. For example, (mean + var) ~ x will display the mean of x above the variance of x. If no function is specified, length is assumed, so the table will display counts. (At most one summary function may be specified in any one term, so mean*var would be an error.)

    you get one-values because you get counts. to get what you want, you should use something similar to this line:

    tabular(temp ~ crop * reg* (impact + se) *   mean , data = pred.table)
    

    I guess this is not the entire solution but something that will bring you closer. let me know in the comments, if s.th. is missing