1

I am trying to create a summary table of the mean ± standard error. So far I have managed to extract the mean and se columns from the SummarySE function, however I am now stuck and can't figure out how to get the column and row names.

Data:

structure(list(TREATMENT = c("A", "A", "A", "B", "B", "B", "C", 
"C", "C", "D", "D", "D", "A", "A", "A", "B", "B", "B", "C", "C", 
"C", "D", "D", "A", "A", "A", "B", "B", "B", "C", "C", "C", "D", 
"D"), TIME = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), `Species 1` = c(1.64963636363636, 
1.6574, 1.58190909090909, 1.6365, 1.75075, 1.83825, 2.247, 1.95557142857143, 
2.35236363636364, 1.82253333333333, 1.37785714285714, 1.37892857142857, 
2.952125, 3.33311111111111, 3.0967, 3.3912, 3.89709090909091, 
3.51207692307692, 6.6176, 8.41072727272727, 7.3625, 7.19133333333333, 
4.88216666666667, 4.770625, 28.371125, 6.8786, 4.10457142857143, 
13.232, 8.66828571428571, 14.6534615384615, 51.7823333333333, 
42.5318333333333, 20.2263333333333, 13.396), `Species 2` = c(2.36021428571429, 
2.30930769230769, 3.01018181818182, 2.0195, 1.97107142857143, 
2.07614285714286, 1.85307142857143, 2.2695, 2.29286666666667, 
1.9504375, 1.95207142857143, 2.2144375, 2.57385714285714, 2.865, 
2.92928571428571, 2.8453, 2.63855555555556, 4.15657142857143, 
2.807375, 3.554, 6.12177777777778, 2.65541666666667, 3.7972, 
1.285, 4.755, 4.271, 2.5925, 2.83158333333333, 4.638, 16.8010833333333, 
13.1841666666667, 20.418, 3.24075, 3.0115), `Species 3` = c(2.1960625, 
2.28214285714286, 3.05583333333333, 2.16986666666667, 2.06655, 
2.54166666666667, 1.81271428571429, 1.89566666666667, 2.3227, 
2.80616666666667, 2.05, 1.97592857142857, 3.44611111111111, 1.745, 
2.9208, 4.6525, 3.89709090909091, 2.69244444444444, 2.7464, 5.93042857142857, 
5.18888888888889, 3.8943125, 4.054, 6.11511111111111, 2.518125, 
5.2216, 5.16908333333333, 2.59, 2.55716666666667, 9.12085714285714, 
31.5835714285714, 17.227, 5.98033333333333, 5.21509090909091), 
`Species 4` = c(2.00981818181818, 2.7604, 3.5535, 2.39085714285714, 
3.00138888888889, 2.835, 2.25311111111111, 3.00791666666667, 
2.55933333333333, 2.13933333333333, 2.331, 2.94761111111111, 
3.66525, 7.304125, 3.14788888888889, 7.37175, 5.1192, 4.46957142857143, 
2.73258333333333, 15.4874166666667, 5.9237, 4.81615384615384, 
3.7743, 8.0213, 28.192, 17.6561111111111, 17.3084375, 7.81390909090909, 
3.849, 9.618, 53.8383, 10.2361111111111, 17.9905454545455, 
5.6655)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-34L), .Names = c("TREATMENT", "TIME", "Species 1", "Species 2", 
"Species 3", "Species 4"))

Attempt:

library(Rmisc)
library(Reshape2)

melt <- melt(example, id=c("TREATMENT", "TIME"), value.name="growth", variable.name = "Species")

x <- summarySE(melt, measurevar = "growth", groupvars = c("TIME", "TREATMENT", "Species") )

xmean<-signif(x[,5],digits=3)

xse<-signif(x[,7],digits=3)

x<-paste(xmean,xse,sep=" \u00b1 ")

x

Produces:

[1] "1.63 ± 0.024"  "2.56 ± 0.226"  "2.51 ± 0.273"  "2.77 ± 0.446"  "1.74 ± 0.0584" "2.02 ± 0.0304" "2.26 ± 0.144"

I have a feeling I am going about this the long way, is there such a thing as a package that creates publication-ready tables? Something like the table-version of ggplot2. I can't seem to find anything.

J.Con
  • 4,101
  • 4
  • 36
  • 64

1 Answers1

2

Hmm, I can't seem to figure out what you're summarizing by. I don't have the Rmisc package, but assuming the answers you're getting are correct, you can just wrap it in a data frame and give each measure a row name:

d=data.frame(summary=x, row.names=colnames(example)[3:ncol(example)])

Which if, say, you were summarizing by species would look like this:

                summary
Species.1  1.852 ± 0.28
Species.2  2.24 ± 0.338
Species.3  2.26 ± 0.371
Species.4 2.708 ± 0.465

EDIT: Since the summarySE output is already a data frame, you just need to create a new column to format the data the way way you want, then reshape it how you want it to look in the end. Here's an example, in keeping with your use of reshape2:

x$summary = paste(round(x$growth, 3), "\u00b1", round(x$se, 3))
dcast(x, TIME+TREATMENT~Species, value.var='summary')

Output:

   TIME TREATMENT       Species 1      Species 2     Species 3       Species 4
1     0         A    1.63 ± 0.024   2.56 ± 0.226 2.511 ± 0.273   2.775 ± 0.446
2     0         B   1.742 ± 0.058   2.022 ± 0.03 2.259 ± 0.144   2.742 ± 0.182
3     0         C   2.185 ± 0.119  2.138 ± 0.143  2.01 ± 0.158   2.607 ± 0.219
4     0         D   1.526 ± 0.148  2.039 ± 0.088 2.277 ± 0.265   2.473 ± 0.244
5     1         A   3.127 ± 0.111  2.789 ± 0.109 2.704 ± 0.503   4.706 ± 1.308
6     1         B     3.6 ± 0.153  3.213 ± 0.475 3.747 ± 0.571   5.654 ± 0.879
7     1         C    7.464 ± 0.52  4.161 ± 1.004 4.622 ± 0.962   8.048 ± 3.832
8     1         D   6.037 ± 1.155  3.226 ± 0.571  3.974 ± 0.08   4.295 ± 0.521
9     2         A    13.34 ± 7.54  3.437 ± 1.085 4.618 ± 1.081  17.956 ± 5.825
10    2         B   8.668 ± 2.635  3.354 ± 0.646 3.439 ± 0.865   9.657 ± 3.993
11    2         C 36.323 ± 11.159 16.801 ± 2.088 19.31 ± 6.568 24.564 ± 14.638
12    2         D  16.811 ± 3.415  3.126 ± 0.115 5.598 ± 0.383  11.828 ± 6.163
teadotjay
  • 1,395
  • 12
  • 15
  • Hi there, thanks for your help. I have now included all the data so you can see what I am doing (I hope). :) – J.Con Jun 18 '16 at 04:49
  • @J.Con OK, I installed the Rmisc package and think I see what you're after now. See if this is what you're looking for. – teadotjay Jun 18 '16 at 05:09
  • Thank you so much. This layout is what I am looking for, but I guess in my mind I had something like the table version of ggplot2, with the table in a sort of aesthetically pleasing grid format. Maybe this doesn't exist. I will edit my question. Thanks again! – J.Con Jun 18 '16 at 05:40
  • 1
    In that case I'd look into knitr and/or reporteRs. The data frame above can be converted to HTML, PDF, or Word using one of those packages. – teadotjay Jun 18 '16 at 06:27
  • 1
    In my experience, it's been better to generate a data frame the maps to the table you want to report and then use a package to convert the data frame to the desired output format. There is so much variability in how people want to create tables that it is really difficult to make a generalized function that keeps everyone happy. Like, @T.J., I recommend you use his code, then use one of the myriad packages to report the data frame. Other options include xtable, htmlTable, pixiedust, ztable. The reproducible research CRAN task view lists lots of others. – Benjamin Jun 18 '16 at 08:34