jim holtman wrote:
Is this what you want:
# split the dataframe by the grouping (z was your sample data)
z.s <- split(z, z[[1]])
# calculate the median
(ans <- lapply(z.s, function(.grp) apply(.grp[,7:9], 2, median)))
$HOR006_3
TC.15_comps IC.16_comps SOC.17_comps
10.549669 4.224790 7.012470
$HOR006_4
TC.15_comps IC.16_comps SOC.17_comps
14.428948 7.557801 6.568626
$HOR006_5
TC.15_comps IC.16_comps SOC.17_comps
22.378523 13.666364 7.290354
do.call(rbind, ans)
TC.15_comps IC.16_comps SOC.17_comps
HOR006_3 10.54967 4.224790 7.012470
HOR006_4 14.42895 7.557801 6.568626
HOR006_5 22.37852 13.666364 7.290354
Isn't this essentially the same as
> aggregate(airquality[1:4],airquality["Month"], median, na.rm=T)
Month Ozone Solar.R Wind Temp
1 5 18 194.0 11.5 66
2 6 23 188.5 9.7 78
3 7 60 253.0 8.6 84
4 8 52 197.5 8.6 82
5 9 23 192.0 10.3 76
?
On Fri, Jun 27, 2008 at 7:19 PM, Bricklemyer, Ross S <[EMAIL PROTECTED]> wrote:
I am having difficulty calculating the median of grouped data. I have 8 to 10
repeated measures per sample and I have successfully used the following code to
calculate the average for each sample.
libs.norm.preds.median[,7:9]<-apply(libs.norm.preds.median[,7:9],MARGIN=2,
FUN=ave,libs.norm.preds.median[,1])
I then use the unique function to collapse the data into one line per sample.
I would also like to calculate the median, standard error, and coefficient of
variation as well. I have not been able to get median to work properly. I
have tried this and variants:
libs.norm.preds.median[,7:9]<-apply(libs.norm.preds.median[,7:9],MARGIN=2,
FUN=median,libs.norm.preds.median[,1])
I receive the following error:
Warning messages:
1: In if (na.rm) x <- x[!is.na(x)] else if (any(is.na(x)))
return(x[FALSE][NA]) :the condition has length > 1 and only the first element
will be used
Here is a subset of my data (tab delimited):
samp.id core field TC IC SOC TC.15 comps IC.16 comps
SOC.17 comps TC.15 comps IC.16 comps SOC.17 comps
HOR006_3 HOR006 HOR 7.157 0 7.157 8.008273281
0.786161341 6.402343153 8.008273281 0.786161341 6.402343153
HOR006_3 HOR006 HOR 7.157 0 7.157 6.258510623
-1.117567268 6.987405984 6.258510623 0 6.987405984
HOR006_3 HOR006 HOR 7.157 0 7.157 14.21306811
7.968072165 6.818917226 14.21306811 7.968072165 6.818917226
HOR006_3 HOR006 HOR 7.157 0 7.157 17.73301788
9.017994045 9.035508792 17.73301788 9.017994045 9.035508792
HOR006_3 HOR006 HOR 7.157 0 7.157 12.54204929
6.285521186 6.052762372 12.54204929 6.285521186 6.052762372
HOR006_3 HOR006 HOR 7.157 0 7.157 10.07603128
3.485872902 6.937777459 10.07603128 3.485872902 6.937777459
HOR006_3 HOR006 HOR 7.157 0 7.157 11.02330763
4.963708049 7.03753441 11.02330763 4.963708049 7.03753441
HOR006_3 HOR006 HOR 7.157 0 7.157 11.02330763
4.963708049 7.03753441 11.02330763 4.963708049 7.03753441
HOR006_3 HOR006 HOR 7.157 0 7.157 9.249550001
1.92641169 7.675586354 9.249550001 1.92641169 7.675586354
HOR006_3 HOR006 HOR 7.157 0 7.157 7.414208739
-0.020533568 7.057048733 7.414208739 0 7.057048733
HOR006_4 HOR006 HOR 11.73 0 11.73 14.42894814
8.998403641 5.752994239 14.42894814 8.998403641 5.752994239
HOR006_4 HOR006 HOR 11.73 0 11.73 13.65284466
6.757373476 6.388413921 13.65284466 6.757373476 6.388413921
HOR006_4 HOR006 HOR 11.73 0 11.73 10.72185703
5.053095924 6.016783029 10.72185703 5.053095924 6.016783029
HOR006_4 HOR006 HOR 11.73 0 11.73 14.68382689
7.557801473 6.667911142 14.68382689 7.557801473 6.667911142
HOR006_4 HOR006 HOR 11.73 0 11.73 2.287381003
-3.074174656 6.654986023 2.287381003 0 6.654986023
HOR006_4 HOR006 HOR 11.73 0 11.73 14.57145428
8.812845515 6.625453309 14.57145428 8.812845515 6.625453309
HOR006_4 HOR006 HOR 11.73 0 11.73 21.12964238
13.27394496 6.568626499 21.12964238 13.27394496 6.568626499
HOR006_4 HOR006 HOR 11.73 0 11.73 19.46136803
8.03100103 6.910126723 19.46136803 8.03100103 6.910126723
HOR006_4 HOR006 HOR 11.73 0 11.73 13.16591198
4.738398449 6.051036242 13.16591198 4.738398449 6.051036242
HOR006_5 HOR006 HOR 20.339 14.383 5.956 24.17001811
15.44634892 8.095868636 24.17001811 15.44634892 8.095868636
HOR006_5 HOR006 HOR 20.339 14.383 5.956 19.17125764
12.28559645 7.468646662 19.17125764 12.28559645 7.468646662
HOR006_5 HOR006 HOR 20.339 14.383 5.956 20.18713584
13.12584843 6.985808635 20.18713584 13.12584843 6.985808635
HOR006_5 HOR006 HOR 20.339 14.383 5.956 25.58402927
18.23958469 6.960777883 25.58402927 18.23958469 6.960777883
HOR006_5 HOR006 HOR 20.339 14.383 5.956 24.04109959
16.32371239 7.12821025 24.04109959 16.32371239 7.12821025
HOR006_5 HOR006 HOR 20.339 14.383 5.956 19.809507
12.28987767 7.290354063 19.809507 12.28987767 7.290354063
HOR006_5 HOR006 HOR 20.339 14.383 5.956 22.37852335
13.66636406 7.814588276 22.37852335 13.66636406 7.814588276
HOR006_5 HOR006 HOR 20.339 14.383 5.956 20.67374067
12.99877903 6.997267952 20.67374067 12.99877903 6.997267952
HOR006_5 HOR006 HOR 20.339 14.383 5.956 24.69721989
16.10787468 8.381673118 24.69721989 16.10787468 8.381673118
*******************************************************************
Ross Bricklemyer
Dept. of Crop and Soil Sciences
Washington State University
251 Johnson Hall
PO Box 646420
Pullman, WA 99164-6420
Work: 509.335.3661
Cell/Home: 406.570.8576
Fax: 509.335.8674
Email: [EMAIL PROTECTED]
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
O__ ---- Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.