Re: [R] median of grouped data

2010-01-27 Thread Girish A.R.

Check the package doBy as well (pretty handy for analysis of grouped data).

library(doBy)
summaryBy(PM~ID,data=d,FUN=median)

HTH,
-Girish
-- 
View this message in context: 
http://n4.nabble.com/median-of-grouped-data-tp1311971p1312386.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] median of grouped data

2010-01-27 Thread Felipe Carrillo
Like this?

d = read.table(textConnection("ID PM
adt_01  25.255
adt_01  225.36
adt_01  14.2325
adt_02  15
adt_02  12.3
adt_03  15.2
adt_03  148.3
adt_03  25.5
adt_03  14.25"),head=T)
d
d$PM <- as.numeric(d$PM)
ddply(d,.(ID),numcolwise(median))



Felipe D. Carrillo  
Supervisory Fishery Biologist  Department of the Interior  US Fish & Wildlife 
Service  California, USA


--- On Wed, 1/27/10, "María del Pilar Márquez"  wrote:

> From: "María del Pilar Márquez" 
> Subject: [R] median of grouped data
> To: r-help@r-project.org
> Date: Wednesday, January 27, 2010, 10:05 AM
> Good morning,
> I would like to know if is possible somebody can help me
> with this please:
> I would like to calculate the median of grouped data, this
> is an example:
> 
> ID    PM
> adt_01  25.255
> adt_01  225.36
> adt_01  14.2325
> adt_02  15
> adt_02  12.3
> adt_03  15.2
> adt_03  148.3
> adt_03  25.5
> adt_03  14.25
> 
> I need the median of adt_01, 02 and 03 , I know that I have
> to split first my data and then ask for the median but the
> code I have is not working.
> 
> Thank you very much if you can help me with the code
> maria
> 
> __
> 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.
> 




__
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.


Re: [R] median of grouped data

2010-01-27 Thread Peter Alspach
Tena koe Maria

?tapply

Specifically:

tapply(yourData$PM, yourData$ID, median)

HTH 

Peter Alspach 

> -Original Message-
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of "María del 
> Pilar Márquez"
> Sent: Thursday, 28 January 2010 7:06 a.m.
> To: r-help@r-project.org
> Subject: [R] median of grouped data
> 
> Good morning,
> I would like to know if is possible somebody can help me with 
> this please:
> I would like to calculate the median of grouped data, this is 
> an example:
> 
> IDPM
> adt_01  25.255
> adt_01  225.36
> adt_01  14.2325
> adt_02  15
> adt_02  12.3
> adt_03  15.2
> adt_03  148.3
> adt_03  25.5
> adt_03  14.25
> 
> I need the median of adt_01, 02 and 03 , I know that I have 
> to split first my data and then ask for the median but the 
> code I have is not working.
> 
> Thank you very much if you can help me with the code maria
> 
> __
> 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.
> 

__
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.


[R] median of grouped data

2010-01-27 Thread María del Pilar Márquez
Good morning,
I would like to know if is possible somebody can help me with this please:
I would like to calculate the median of grouped data, this is an example:

IDPM
adt_01  25.255
adt_01  225.36
adt_01  14.2325
adt_02  15
adt_02  12.3
adt_03  15.2
adt_03  148.3
adt_03  25.5
adt_03  14.25

I need the median of adt_01, 02 and 03 , I know that I have to split first my 
data and then ask for the median but the code I have is not working.

Thank you very much if you can help me with the code
maria

__
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.


Re: [R] median of grouped data

2008-06-30 Thread [EMAIL PROTECTED]
On 28 июн, 03:19, "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])
>

You might want to create a wrapper around "ave" like this:

myave <- function(..., ave.FUN = mean)
  ave(..., FUN = ave.FUN)

and pass needed ave.FUN parameter to "apply".

Andrey

__
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.


Re: [R] median of grouped data

2008-06-28 Thread Peter Dalgaard

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.37852313.666364 7.290354

  

do.call(rbind, ans)


 TC.15_comps IC.16_comps SOC.17_comps
HOR006_310.549674.224790 7.012470
HOR006_414.428957.557801 6.568626
HOR006_522.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 518   194.0 11.5   66
2 623   188.5  9.7   78
3 760   253.0  8.6   84
4 852   197.5  8.6   82
5 923   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 corefield   TC  IC  SOC TC.15 comps IC.16 comps 
SOC.17 compsTC.15 comps IC.16 comps SOC.17 comps
HOR006_3HOR006  HOR 7.157   0   7.157   8.008273281 
0.786161341 6.402343153 8.008273281 0.786161341 6.402343153
HOR006_3HOR006  HOR 7.157   0   7.157   6.258510623 
-1.1175672686.987405984 6.258510623 0   6.987405984
HOR006_3HOR006  HOR 7.157   0   7.157   14.21306811 
7.968072165 6.818917226 14.21306811 7.968072165 6.818917226
HOR006_3HOR006  HOR 7.157   0   7.157   17.73301788 
9.017994045 9.035508792 17.73301788 9.017994045 9.035508792
HOR006_3HOR006  HOR 7.157   0   7.157   12.54204929 
6.285521186 6.052762372 12.54204929 6.285521186 6.052762372
HOR006_3HOR006  HOR 7.157   0   7.157   10.07603128 
3.485872902 6.93459 10.07603128 3.485872902 6.93459
HOR006_3HOR006  HOR 7.157   0   7.157   11.02330763 
4.963708049 7.03753441  11.02330763 4.963708049 7.03753441
HOR006_3HOR006  HOR 7.157   0   7.157   11.02330763 
4.963708049 7.03753441  11.02330763 4.963708049 7.03753441
HOR006_3HOR006  HOR 7.157   0   7.157   9.249550001 
1.92641169  7.675586354 9.249550001 1.92641169  7.675586354
HOR006_3HOR006  HOR 7.157   0   7.157   7.414208739 
-0.0205335687.057048733 7.414208739 0   7.057048733
HOR006_4HOR006  HOR 11.73   0   11.73   14.42894814 
8.998403641 5.752994239 14.42894814 8.998403641 5.752994239
HOR006_4HOR006  HOR 11.73   0   11.73   13.65284466 
6.757373476 6.388413921 13.65284466 6.757373476 6.388413921
HOR006_4HOR006  HOR 11.73   0   11.73   10.72185703 
5.053095924 6.016783029 10.72185703 5.053095924 6.016783029
HOR006_4HOR006  HOR 11.73   0   11.73   14.68382689 
7.557801473 6.667911142 14.68382689 7.557801473 6.667911142
HOR006_4HOR006  HOR 11.73   0   11.73   2.287381003 
-3.0741746566.654986023 2.287381003 0   6.654986023
HOR006_4HOR006  HOR 11.73   0   11.73   14.57145428 
8.812845515 6.625453309 14.57145428 8.812845515 6.625453309
HOR006_4HOR006  HOR 11.73   0   11.73   21.12964238 
13.27394496 6.568626499 21.12964238 13.27394496 6.568626499
HOR006_4HOR006  HOR 11.73   0   11.73   19.46136803 
8.03100103  6.910126723 19.46136803 8.03100103  6.910126723
HOR006_4HOR006  HOR 11.73   0   11.73   13.16591198 
4.738398449 6.051036242 13.16591198 4.738398449 

Re: [R] median of grouped data

2008-06-27 Thread jim holtman
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.37852313.666364 7.290354

> do.call(rbind, ans)
 TC.15_comps IC.16_comps SOC.17_comps
HOR006_310.549674.224790 7.012470
HOR006_414.428957.557801 6.568626
HOR006_522.37852   13.666364 7.290354


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 corefield   TC  IC  SOC TC.15 comps IC.16 comps   
>   SOC.17 compsTC.15 comps IC.16 comps SOC.17 comps
> HOR006_3HOR006  HOR 7.157   0   7.157   8.008273281 
> 0.786161341 6.402343153 8.008273281 0.786161341 6.402343153
> HOR006_3HOR006  HOR 7.157   0   7.157   6.258510623 
> -1.1175672686.987405984 6.258510623 0   6.987405984
> HOR006_3HOR006  HOR 7.157   0   7.157   14.21306811 
> 7.968072165 6.818917226 14.21306811 7.968072165 6.818917226
> HOR006_3HOR006  HOR 7.157   0   7.157   17.73301788 
> 9.017994045 9.035508792 17.73301788 9.017994045 9.035508792
> HOR006_3HOR006  HOR 7.157   0   7.157   12.54204929 
> 6.285521186 6.052762372 12.54204929 6.285521186 6.052762372
> HOR006_3HOR006  HOR 7.157   0   7.157   10.07603128 
> 3.485872902 6.93459 10.07603128 3.485872902 6.93459
> HOR006_3HOR006  HOR 7.157   0   7.157   11.02330763 
> 4.963708049 7.03753441  11.02330763 4.963708049 7.03753441
> HOR006_3HOR006  HOR 7.157   0   7.157   11.02330763 
> 4.963708049 7.03753441  11.02330763 4.963708049 7.03753441
> HOR006_3HOR006  HOR 7.157   0   7.157   9.249550001 
> 1.92641169  7.675586354 9.249550001 1.92641169  7.675586354
> HOR006_3HOR006  HOR 7.157   0   7.157   7.414208739 
> -0.0205335687.057048733 7.414208739 0   7.057048733
> HOR006_4HOR006  HOR 11.73   0   11.73   14.42894814 
> 8.998403641 5.752994239 14.42894814 8.998403641 5.752994239
> HOR006_4HOR006  HOR 11.73   0   11.73   13.65284466 
> 6.757373476 6.388413921 13.65284466 6.757373476 6.388413921
> HOR006_4HOR006  HOR 11.73   0   11.73   10.72185703 
> 5.053095924 6.016783029 10.72185703 5.053095924 6.016783029
> HOR006_4HOR006  HOR 11.73   0   11.73   14.68382689 
> 7.557801473 6.667911142 14.68382689 7.557801473 6.667911142
> HOR006_4HOR006  HOR 11.73   0   11.73   2.287381003 
> -3.0741746566.654986023 2.287381003 0   6.654986023
> HOR006_4HOR006  HOR 11.73   0   11.73   14.57145428 
> 8.812845515 6.625453309 14.57145428 8.812845515 6.625453309
> HOR006_4HOR006  HOR 11.73   0   11.73   21.12964238 
> 13.27394496 6.568626499 21.12964238 13.27394496 6.568626499
> HOR006_4HOR006  HOR 11.73   0   11.73   19.46136803 
> 8.03100103  6.910126723 19.46136803 8.03100103  6.910126723
> HOR006_4HOR006  HOR 11.73   0   11.73   13.16591198 
> 4.738398449 6.051036242 13.16591198 4.738398449 6.051036242
> HOR006_5HOR006  HOR 20.339  14.383  5.956   24.17001811 
> 15.44634892 8.095868636 24.17001811 15.44634892 8.095868636
> HOR006_5HOR006  HOR 20.339  14.383

[R] median of grouped data

2008-06-27 Thread Bricklemyer, Ross S
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 corefield   TC  IC  SOC TC.15 comps IC.16 comps 
SOC.17 compsTC.15 comps IC.16 comps SOC.17 comps
HOR006_3HOR006  HOR 7.157   0   7.157   8.008273281 
0.786161341 6.402343153 8.008273281 0.786161341 6.402343153
HOR006_3HOR006  HOR 7.157   0   7.157   6.258510623 
-1.1175672686.987405984 6.258510623 0   6.987405984
HOR006_3HOR006  HOR 7.157   0   7.157   14.21306811 
7.968072165 6.818917226 14.21306811 7.968072165 6.818917226
HOR006_3HOR006  HOR 7.157   0   7.157   17.73301788 
9.017994045 9.035508792 17.73301788 9.017994045 9.035508792
HOR006_3HOR006  HOR 7.157   0   7.157   12.54204929 
6.285521186 6.052762372 12.54204929 6.285521186 6.052762372
HOR006_3HOR006  HOR 7.157   0   7.157   10.07603128 
3.485872902 6.93459 10.07603128 3.485872902 6.93459
HOR006_3HOR006  HOR 7.157   0   7.157   11.02330763 
4.963708049 7.03753441  11.02330763 4.963708049 7.03753441
HOR006_3HOR006  HOR 7.157   0   7.157   11.02330763 
4.963708049 7.03753441  11.02330763 4.963708049 7.03753441
HOR006_3HOR006  HOR 7.157   0   7.157   9.249550001 
1.92641169  7.675586354 9.249550001 1.92641169  7.675586354
HOR006_3HOR006  HOR 7.157   0   7.157   7.414208739 
-0.0205335687.057048733 7.414208739 0   7.057048733
HOR006_4HOR006  HOR 11.73   0   11.73   14.42894814 
8.998403641 5.752994239 14.42894814 8.998403641 5.752994239
HOR006_4HOR006  HOR 11.73   0   11.73   13.65284466 
6.757373476 6.388413921 13.65284466 6.757373476 6.388413921
HOR006_4HOR006  HOR 11.73   0   11.73   10.72185703 
5.053095924 6.016783029 10.72185703 5.053095924 6.016783029
HOR006_4HOR006  HOR 11.73   0   11.73   14.68382689 
7.557801473 6.667911142 14.68382689 7.557801473 6.667911142
HOR006_4HOR006  HOR 11.73   0   11.73   2.287381003 
-3.0741746566.654986023 2.287381003 0   6.654986023
HOR006_4HOR006  HOR 11.73   0   11.73   14.57145428 
8.812845515 6.625453309 14.57145428 8.812845515 6.625453309
HOR006_4HOR006  HOR 11.73   0   11.73   21.12964238 
13.27394496 6.568626499 21.12964238 13.27394496 6.568626499
HOR006_4HOR006  HOR 11.73   0   11.73   19.46136803 
8.03100103  6.910126723 19.46136803 8.03100103  6.910126723
HOR006_4HOR006  HOR 11.73   0   11.73   13.16591198 
4.738398449 6.051036242 13.16591198 4.738398449 6.051036242
HOR006_5HOR006  HOR 20.339  14.383  5.956   24.17001811 
15.44634892 8.095868636 24.17001811 15.44634892 8.095868636
HOR006_5HOR006  HOR 20.339  14.383  5.956   19.17125764 
12.28559645 7.468646662 19.17125764 12.28559645 7.468646662
HOR006_5HOR006  HOR 20.339  14.383  5.956   20.18713584 
13.12584843 6.985808635 20.18713584 13.12584843 6.985808635
HOR006_5HOR006  HOR 20.339  14.383  5.956   25.58402927 
18.23958469 6.960777883 25.58402927 18.23958469 6.960777883
HOR006_5HOR006  HOR 20.339  14.383  5.956   24.04109959 
16.32371239 7.12821025  24.04109959 16.32371239 7.12821025
HOR006_5HOR006  HOR 20.339  14.383  5.956   19.809507   
12.28987767 7.290354063 19.809507   12.28987767 7.290354063
HOR006_5HOR006  HOR 20.339  14.383  5.956   22.37852335 
13.66636406 7.814588276 22.37852335 13.66636406 7.814588276
HOR006_5HOR006  HOR 20.339  14