Summarise with dplyr and export table with means and sd (+/-)

490 views Asked by At

I have a data frame with different observations in three compartments (COMP1, COMP2 and COMP3) for ten products (PRO01-PRO10) and for three different treatments (A, B and C).

My data is this:

TRA PRO COMP1   COMP2   COMP3
A   PRO01   2.98    6.35    45.57
A   PRO01   2.97    9.17    66.52
A   PRO01   3.83    4.95    62.25
A   PRO01   3.13    5.05    53.24
A   PRO01   2.65    7.44    62.53
B   PRO01   4.13    3.77    30.62
B   PRO01   4.88    6.71    26.45
B   PRO01   4.41    5.16    26.89
B   PRO01   3.43    5.02    37.97
B   PRO01   3.62    8.98    53.30
C   PRO01   7.80    1.56    13.43
C   PRO01   6.00    1.45    9.12
C   PRO01   4.96    2.07    21.03
C   PRO01   5.31    2.69    29.47
C   PRO01   5.44    3.48    13.44
A   PRO02   1.43    9.83    38.89
A   PRO02   1.78    11.77   33.32
A   PRO02   2.88    9.77    35.90
A   PRO02   1.94    10.36   20.44
A   PRO02   1.36    8.42    41.19
B   PRO02   1.58    6.72    28.08
B   PRO02   1.61    8.68    17.38
B   PRO02   1.63    10.29   12.18
B   PRO02   2.91    10.21   26.27
B   PRO02   2.07    11.94   14.65
C   PRO02   2.57    8.27    27.41
C   PRO02   2.32    3.89    14.36
C   PRO02   2.10    7.94    22.88
C   PRO02   1.45    10.51   17.00
C   PRO02   2.83    9.03    20.88
A   PRO03   2.14    89.90   58.88
A   PRO03   1.81    102.86  51.40
A   PRO03   2.45    74.03   49.40
A   PRO03   2.03    65.10   39.31
A   PRO03   1.28    78.75   42.23
B   PRO03   2.48    36.88   26.95
B   PRO03   2.42    37.56   22.73
B   PRO03   3.27    59.76   33.86
B   PRO03   2.36    48.30   35.14
B   PRO03   3.45    63.23   36.72
C   PRO03   3.18    22.96   22.02
C   PRO03   2.65    28.82   19.18
C   PRO03   4.88    40.53   35.97
C   PRO03   3.03    28.96   33.17
C   PRO03   4.23    35.33   40.81
A   PRO04   2.92    4.23    82.84
A   PRO04   3.62    2.47    70.02
A   PRO04   2.71    3.06    77.18
A   PRO04   2.51    2.80    62.45
A   PRO04   3.20    1.77    41.31
B   PRO04   2.66    0.68    41.87
B   PRO04   2.70    0.31    18.29
B   PRO04   2.41    0.54    59.70
B   PRO04   3.25    2.02    44.16
B   PRO04   2.77    1.78    27.64
C   PRO04   3.29    1.42    14.96
C   PRO04   2.74    1.15    25.03
C   PRO04   3.39    0.86    25.51
C   PRO04   3.21    1.15    21.64
C   PRO04   3.56    2.10    15.43
A   PRO05   1.93    2.23    32.19
A   PRO05   2.52    1.62    51.84
A   PRO05   1.47    1.47    29.25
A   PRO05   1.58    1.95    39.85
A   PRO05   1.78    0.86    32.98
B   PRO05   2.16    0.71    21.74
B   PRO05   2.84    1.94    13.62
B   PRO05   2.79    1.48    28.82
B   PRO05   2.30    1.42    17.51
B   PRO05   1.51    1.64    24.45
C   PRO05   3.35    0.67    15.68
C   PRO05   3.10    0.32    8.38
C   PRO05   4.84    0.37    10.17
C   PRO05   3.24    0.36    17.01
C   PRO05   4.33    1.34    15.34
A   PRO06   2.16    -3.65   79.09
A   PRO06   1.92    -3.61   95.17
A   PRO06   2.71    -6.52   52.78
A   PRO06   3.22    0.39    56.68
A   PRO06   3.42    -0.23   54.12
B   PRO06   3.99    -3.48   41.08
B   PRO06   3.84    2.42    24.22
B   PRO06   2.47    -2.72   56.12
B   PRO06   2.77    -1.21   59.08
B   PRO06   4.40    -3.28   56.26
C   PRO06   3.71    1.65    42.51
C   PRO06   4.28    -2.94   33.93
C   PRO06   4.19    -2.78   39.77
C   PRO06   4.85    1.57    46.09
C   PRO06   4.97    -1.41   39.10
A   PRO07   2.27    41.08   65.72
A   PRO07   2.90    47.04   40.57
A   PRO07   1.84    55.85   96.81
A   PRO07   1.74    37.22   47.55
A   PRO07   1.76    28.25   65.13
B   PRO07   3.01    32.36   27.66
B   PRO07   2.13    29.75   77.22
B   PRO07   2.60    29.42   66.03
B   PRO07   3.73    28.67   35.98
B   PRO07   3.02    37.01   62.55
C   PRO07   3.55    26.95   31.56
C   PRO07   2.58    17.78   57.25
C   PRO07   5.11    18.35   45.09
C   PRO07   3.48    11.95   16.57
C   PRO07   3.32    20.85   49.18
A   PRO08   3.65    50.45   42.71
A   PRO08   4.55    50.47   48.68
A   PRO08   3.04    41.17   48.24
A   PRO08   2.90    55.42   44.13
A   PRO08   2.94    56.21   62.36
B   PRO08   4.59    44.09   35.45
B   PRO08   3.38    48.81   45.27
B   PRO08   4.05    24.22   29.42
B   PRO08   5.54    44.77   40.34
B   PRO08   4.60    23.49   25.07
C   PRO08   5.34    17.37   20.61
C   PRO08   4.08    20.39   37.25
C   PRO08   7.49    28.17   44.26
C   PRO08   5.32    25.32   37.20
C   PRO08   5.12    23.97   27.32
A   PRO09   4.88    3.52    155.02
A   PRO09   5.01    3.73    64.61
A   PRO09   5.32    2.48    122.97
A   PRO09   5.04    2.23    90.67
A   PRO09   5.42    3.41    103.11
B   PRO09   7.02    1.45    47.06
B   PRO09   6.00    3.80    38.62
B   PRO09   5.67    5.33    33.63
B   PRO09   5.86    1.66    68.59
B   PRO09   5.62    2.90    103.50
C   PRO09   8.05    1.03    28.59
C   PRO09   7.72    2.90    27.05
C   PRO09   7.53    1.20    71.29
C   PRO09   5.69    0.21    35.89
C   PRO09   8.99    2.28    55.69
A   PRO10   1.92    41.66   70.39
A   PRO10   2.31    43.47   38.09
A   PRO10   1.18    38.67   40.64
A   PRO10   1.97    44.37   63.86
A   PRO10   2.88    24.10   30.82
B   PRO10   3.27    25.34   29.49
B   PRO10   1.99    34.94   46.29
B   PRO10   3.13    42.81   52.02
B   PRO10   3.24    23.55   37.69
B   PRO10   3.42    19.18   24.54
C   PRO10   3.86    10.46   46.76
C   PRO10   2.71    8.41    27.98
C   PRO10   4.40    5.92    51.34
C   PRO10   3.78    7.69    20.83
C   PRO10   5.38    13.08   18.61

With dplyr it is very easy to calculate the mean and sd for the different compartments, treatments and products. But I would like to export these results as a table to paste it in Word where appeared for each treatment the means of each product and compartment the mean (+/-) the sd. However I tried with Rmarkdown but I couldn't do it.

table <- read.table("clipboard", header=T, sep="\t", dec=".")
df <- data.frame(table)

dfc <- group_by(df, PRO, TRA) %>%
  summarise(meanComp1 = mean (COMP1),
            sdComp1 = sd (COMP1),
            meanComp2 = mean (COMP2),
            sdComp2 = sd (COMP2),
            meanComp3 = mean (COMP3),
            sdComp3 = sd (COMP3))
dfc

Does anyone know how to do it? I had check other questions but it is the first one using the dplyr results and I think it can be useful for others.

1

There are 1 answers

1
JohannesNE On BEST ANSWER

Do you mean like this?

library(knitr)

dfc[c(-1,-2)] <- signif(dfc[c(-1,-2)], digits = 4)

dfc_str <- transmute(dfc, TRA, 
             Comp1 = paste(meanComp1, " +/-", sdComp1),
             Comp2 = paste(meanComp2, " +/-", sdComp2),
             Comp3 = paste(meanComp3, " +/-", sdComp3))

kable(dfc_str)