[R] Calculations with aggregate data: mean +/- standard deviation

Rui Barradas ruipbarradas at sapo.pt
Thu Mar 20 20:27:08 CET 2014


Hello,

Try

res[order(res[,1]), ]

Hope this helps,

Rui Barradas

Em 20-03-2014 17:48, Luigi Marongiu escreveu:
> Dear Rui,
> Thnak you very much: this works exactly as I requested, but the merged
> vetor (res) order the variables by alphabetical order rather than by the
> factors I indicated during the definition of the data frame; thus AVG,
> SD and res have different row order.
> When I tried to define the factors of AVG, SD and resthe original order
> is not kept:
>  > AVG
>     stimulation       copy
> 1 Unstimulated   1470.080
> 2        ESAT6   2043.772
> 3        CFP10   6107.652
> 4      Rv3615c   1639.437
> 5       Rv2654   4025.472
> 6       Rv3879   2558.704
> 7       Rv3873   3538.781
> 8          PHA 213613.795
> AVG$stimulation<-factor(AVG$stimulation, levels = c("Unstimulated",
> "ESAT6", "CFP10", "Rv3615c",
> "Rv2654", "Rv3879", "Rv3873", "PHA"))
> SD$stimulation<-factor(SD$stimulation, levels = c("Unstimulated",
> "ESAT6", "CFP10", "Rv3615c",
> "Rv2654", "Rv3879", "Rv3873", "PHA"))
> res$stimulation<-factor(res$stimulation, levels = c("Unstimulated",
> "ESAT6", "CFP10", "Rv3615c",
> "Rv2654", "Rv3879", "Rv3873", "PHA"))
>  > res
>     stimulation        AVG         SD        lower       upper
> 1        CFP10   6107.652   9597.686   -3490.0334   15705.338
> 2        ESAT6   2043.772   3674.133   -1630.3606    5717.905
> 3          PHA 213613.795 789614.399 -576000.6044 1003228.194
> 4       Rv2654   4025.472   5933.346   -1907.8739    9958.818
> 5      Rv3615c   1639.437   2546.439    -907.0025    4185.876
> 6       Rv3873   3538.781   6205.863   -2667.0813    9744.644
> 7       Rv3879   2558.704   5812.945   -3254.2414    8371.649
> 8 Unstimulated   1470.080   1698.197    -228.1178    3168.277
> May I ask whether there is a method to solve this discrepancy?
> best regards,
> Luigi
>
>
>
> On Wed, Mar 19, 2014 at 6:50 PM, Rui Barradas <ruipbarradas at sapo.pt
> <mailto:ruipbarradas at sapo.pt>> wrote:
>
>     Hello,
>
>     Maybe something like the following.
>
>     res <- merge(AVG, SD, by = "stimulation")
>     names(res)[2:3] <- c("AVG", "SD")
>     res$lower <- res$AVG - res$SD
>     res$upper <- res$AVG + res$SD
>     res
>
>
>     Hope this helps,
>
>     Rui Barradas
>
>     Em 19-03-2014 18:15, Luigi Marongiu escreveu:
>
>         Dear all,
>         I have obtained the averages (means) and standard deviations (SD) of
>         different variable using the aggregate function, which I have
>         found very
>         useful for these kind of computation.
>         However I would like to calculate the lower and upper ends of the
>         data (that is mean - SD and mean + SD) varible-wise, but I don't
>         know how
>         to cope with this aggregate data. I cannot simply subtract the
>         aggregated
>         results (which I have called AVG and SD) and the aggregate
>         function is a
>         bit too complicated for me.
>         Does anybody knows how to add and subtract the means and standard
>         deviations obtained using the aggregate function?
>
>         Best regards,
>         Luigi
>
>
>            my.data<-structure(list(
>              column_1 = 1:120,
>              column_2 = structure(c(
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8,
>            1,2,3,4,5,6,7,8), .Label = c("Unstimulated", "ESAT6",
>         "CFP10", "Rv3615c",
>         "Rv2654", "Rv3879", "Rv3873", "PHA"), class = "factor"),
>                column_3 = c(
>            192.0519108,183.6403531,53.46798757
>         <tel:53.46798757>,83.__60638077,69.60749873,159.__4706861,256.8765622
>         <tel:256.8765622>,499.__2899303,
>            2170.799076,1411.349719,2759.__472348,2098.973397
>         <tel:2098.973397>,2164.739515
>         <tel:2164.739515>,1288.676574,1611.__486543,6205.229575
>         <tel:6205.229575>,
>         870.7424981
>         <tel:870.7424981>,465.9967135,191.8962375,864.__0937485,2962.693675,1289.__259137,2418.651212,7345.__712517,
>
>         0,168.1198893,674.4342961,101.__1575401,47.81596237,0,0,1420.__793922,
>            142.6871331,5.466468742,291.__9564635,80.73914133
>         <tel:80.73914133>,73.02239621,64.__47806871,144.3543635,3167.__959757,
>         3164.748333
>         <tel:3164.748333>,1092.634557,28733.20269,1207.__87783,729.6090973,151.8706088,__241.2466141,9600.963594,
>            1411.718287,12569.96285,1143.__254476,6317.378481
>         <tel:6317.378481>,16542.27718,79.__68025792,1958.495138,7224.__503437,
>         208.4382941
>         <tel:208.4382941>,69.48609769,656.691151,0.__499017582,7114.910926,187.__6296174,41.73980805
>         <tel:41.73980805>,8930.__784541,
>            4.276752185,0.432300363,60.89228665
>         <tel:60.89228665>,1.__103924786,0.490686366,1.__812993239,7.264531581,1518.__610307,
>         2172.051528
>         <tel:2172.051528>,595.8513744,17141.84336,589.__6565971,1340.287628,117.__350942,593.7034054,24043.__61463,
>            0,81.83292179
>         <tel:81.83292179>,1539.864321,36.41722958,8.__385131047,161.7647376,65.__21615696,7265.573875,
>         97.84753179
>         <tel:97.84753179>,154.051827,0.613835842,10.__06138851,45.04879285
>         <tel:45.04879285>,176.8284258,18795.__75462,3067686.769
>         <tel:3067686.769>,
>            5780.34957,944.2200834,2398.235596
>         <tel:2398.235596>,1083.__393165,2541.714557,1251.__670895,1547.178549,1792.__679176,
>         3067.988416 <tel:3067.988416>,8117.210173,23676.02226
>         <tel:23676.02226>,8251.937547,__17360.80494,18563.61561,16941.__865,31453.96708
>         <tel:31453.96708>,
>         2767.493803 <tel:2767.493803>,4796.33016,12292.93705,3864.657567
>         <tel:3864.657567>,9380.__673835,14886.44683,8457.88646,__26050.47191
>         <tel:26050.47191>)),
>         .Names = c("row", "stimulation", "copy"), row.names = c(NA, -120L),
>            class = "data.frame")
>         attach(my.data)
>
>         AVG<-aggregate(copy ~ stimulation  , my.data, mean, na.rm = T)
>         SD<-aggregate(copy ~ stimulation  , my.data, sd, na.rm = T)
>
>         # ??? question: AVG-SD and AVG + SD ???
>
>                  [[alternative HTML version deleted]]
>
>         ________________________________________________
>         R-help at r-project.org <mailto:R-help at r-project.org> mailing list
>         https://stat.ethz.ch/mailman/__listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>         PLEASE do read the posting guide
>         http://www.R-project.org/__posting-guide.html
>         <http://www.R-project.org/posting-guide.html>
>         and provide commented, minimal, self-contained, reproducible code.
>
>




More information about the R-help mailing list