How to print a long column from a dataframe splited in two columns side by side

自古美人都是妖i 提交于 2019-12-23 04:51:15

问题


I have a dataframe with 60 rows of data, here I'm posting a single column of it, so we have some data to work:

    local   N
1    IPA   0.164
2    IPA   0.045
3    IPA   0.069
4    IPA   0.100
5    IPA   0.050
.     .      .
.     .      .
.     .      . 
28   IPA   0.078
29   IPA   0.121
30   IPA   0.104
31   OPA   0.035
32   OPA   0.057
33   OPA   0.043
.     .      .
.     .      .
.     .      .
55   OPA   0.021
56   OPA   0.004
57   OPA   0.043
58   OPA   0.002
59   OPA   0.005
60   OPA   0.034

Observe that the local for the 30 first reading is IPA and the local for the last 30 readings is OPA. I'm using knitr to make a pdf file with all my analysis in R, however if I print this column, I get a very long table that occupies a whole page.

stargazer(data[c("local", "N")], summary=FALSE)

I'm using stargazer, but I can also do it with other packages. My goal is to divide the printed table in two columns so it can have better dimensions and fit better my text. I'm trying to get something lie this:

    local    N          local   N
1    IPA   0.164   31   OPA   0.035
2    IPA   0.045   32   OPA   0.057
3    IPA   0.069   33   OPA   0.043
4    IPA   0.100   34   OPA   0.029
5    IPA   0.050   35   OPA   0.017
.     .      .     .     .      .
.     .      .     .     .      .
.     .      .     .     .      .
25   IPA   0.141   55   OPA   0.021
26   IPA   0.100   56   OPA   0.004
27   IPA   0.104   57   OPA   0.043
28   IPA   0.078   58   OPA   0.002
29   IPA   0.121   59   OPA   0.005
30   IPA   0.104   60   OPA   0.034

I'm diving 30 rown to the left and 30 to the right because the local for the 30 first readings (IPA) is different from the local to the 30 last readings (OPA). An even better achievement for me will be to have the local as a name of the table, something like:

    IPA N        OPA N
1   0.164   31   0.035
2   0.045   32   0.057
3   0.069   33   0.043
4   0.100   34   0.029
5   0.050   35   0.017
.     .     .      .  
.     .     .      .  
.     .     .      .  
25   0.141   55   0.021
26   0.100   56   0.004
27   0.104   57   0.043
28   0.078   58   0.002
29   0.121   59   0.005
30   0.104   60   0.034

I have tried to make stargazer understand the data I'm sending to it as a 2 column table, but I'm not having the desired result

stargazer(c(data[grep("IPA", data$local), "N"],
            data[grep("OPA", data$local), "N"]),
          summary=FALSE)

This results in a very long row, actually. :/

来源:https://stackoverflow.com/questions/40694757/how-to-print-a-long-column-from-a-dataframe-splited-in-two-columns-side-by-side

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!