双向表与表生成1' s

时间:2017-08-20 07:35:04

标签: r tabular two-way

我试图用tabluar()创建一个简单的双向表,但我得到的只是输出中的1。

为什么我的估计值没有出现,而是我得到1?

示例代码

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"))

制作表格

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

输出

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

1 个答案:

答案 0 :(得分:2)

我认为你的问题在于代码的这一部分:

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

如果您阅读?tabular的文档,则会发现该段落:

  

如果术语求值为函数,则它应该是摘要函数   当应用于值向量时产生标量值,和   标量将显示在表格中。例如,(mean + var)   ~x将在x的方差之上显示x的平均值。如果没有功能   如果指定了长度,则表格将显示计数。 (在   大多数一个汇总函数可以在任何一个术语中指定,所以   mean * var将是一个错误。)

你得到一个值,因为你得到了数。为了得到你想要的东西,你应该使用类似于这一行的东西:

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

我想这不是整个解决方案,但会让你更接近。请在评论中告诉我,如果是s.th.缺少