R中的“表”包

时间:2014-05-21 00:06:14

标签: r

我正在尝试使用"表格"在R中打包,以便在tex中放置一个漂亮的表。

这是我的原始数据框(让我们称之为"示例")。

   type year High-Low coeff.star  sd.star
1   Emp    2      Low     0.0099 (0.0098)
2   Emp    2     High      0.006 (0.0098)
7   Ind    2      Low  0.0357*** (0.0088)
8   Ind    2     High  0.0309*** (0.0087)
3   Emp    5      Low     0.0128 (0.0209)
4   Emp    5     High     0.0031 (0.0208)
9   Ind    5      Low  0.0869*** (0.0198)
10  Ind    5     High  0.0743*** (0.0196)

我的初衷是将此数据框转换为像这样的表

           Emp                  Ind
       Low     High   |      Low     High
2year 0.0099  0.006   |  0.0357*** 0.0309***
     (0.0098)(0.0098) |  (0.0088) (0.0087)
--------------------------------------------
5year 0.0128  0.0031  |  0.0869*** 0.0743***
     (0.0209)(0.0208) |  (0.0198) (0.0196)

但是,我正在努力学习如何熟练使用这个表包,我觉得很难实现它。如果做起来很难......我想借用你的智慧来展示这样一张桌子......

           Emp                  Ind
       Low     High         Low     High
2year 0.0099  0.006     0.0357*** 0.0309***
5year 0.0128  0.0031    0.0869*** 0.0743***

1 个答案:

答案 0 :(得分:0)

所以我认为我非常接近,但它非常混乱。我必须使用rehape2。这就是我得到的。首先,输入数据

library(tables)
library(reshape2)

dd <-data.frame(
    type="c("Emp", "Emp", "Ind", "Ind", "Emp", "Emp", "Ind", "Ind"),
    year = c(2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L),
    High.Low = c("Low", "High", "Low", "High", "Low", "High", "Low", "High"),
    coeff.star= c("0.0099", "0.006", "0.0357***", "0.0309***", 
        "0.0128", "0.0031", "0.0869***", "0.0743***"),
    sd.star = c("(0.0098)", "(0.0098)", "(0.0088)", "(0.0087)", 
        "(0.0209)", "(0.0208)", "(0.0198)", "(0.0196)")
)

#reshape and check types
mm<-melt(dd, c("type", "year", "High.Low"), 
    measure.vars=c("coeff.star", "sd.star"))
mm$year<-factor(mm$year)
mm$High.Low<-factor(mm$High.Low, levels=c("Low","High"))

这里我不得不将数据融化一点,以便“星”列的头部有自己的行。接下来我使用了表格

tabular(year*Heading()*variable ~  
    Heading()*type*Heading()*High.Low*value*Heading()*identity, mm)

这给了这个结果

                 Emp               Ind                
                 Low      High     Low       High     
 year            value    value    value     value    
 2    coeff.star 0.0099   0.006    0.0357*** 0.0309***
      sd.star    (0.0098) (0.0098) (0.0088)  (0.0087) 
 5    coeff.star 0.0128   0.0031   0.0869*** 0.0743***
      sd.star    (0.0209) (0.0208) (0.0198)  (0.0196) 

希望这会让你非常接近。