[R] median of grouped data
Bricklemyer, Ross S
rsb at wsu.edu
Sat Jun 28 01:19:51 CEST 2008
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: rsb at wsu.edu
More information about the R-help
mailing list