[R] Finding the maximum in a particular group in a dataframe

David Winsemius dwinsemius at comcast.net
Wed Feb 2 14:51:46 CET 2011


On Feb 2, 2011, at 3:48 AM, Asan Ramzan wrote:

> Hello
>
> I am trying to find a way to find the max value, for only a subset  
> of a
> dataframe, depending on how the data is grouped for example,
>
> How would I find the maxmium responce, for all the GPR119a condition  
> below:
>
> I've tried tapply
>
>> tapply(GPR119data$responce, GPR119data$GPR119a, max)
> Error in tapply(GPR119data$responce, GPR119data$GPR119a, max) :
>   arguments must have same length
>
You need to use tapply on the names of the columns , not on the values:

tapply(GPR119data$responce, GPR119data$condition, max)

 > tapply(GPR119data$responce, GPR119data$condition, max)
       con   GPR119a   GPR119b   GPR119c   GPR119d   GPR119e   GPR119f
0.3350084 0.6451204 0.8240356 0.5729588 0.3099644 0.4677268 1.0184191

Then you can choose the GPR119a  value for further examination or  
display;

 > tapply(GPR119data$responce, GPR119data$condition, max)["GPR119a"]
   GPR119a
0.6451204

> responce,mouce,condition

I'm guessing from this display that when you read this data in you  
used the default separator which is white-space and that does not  
include commas. In addition to the above, try adding sep="," in your  
read.table function.

> 0.105902,KO,con
> 0.232018561,KO,con
> 0.335008375,KO,con
> 0.387025433,KO,GPR119a
> 0.576769897,KO,GPR119a
> 0.645120419,KO,GPR119a
> 0.2538608,KO,GPR119b
> 0.183061952,KO,GPR119b
> 0.824035587,KO,GPR119b
> 0.399201597,KO,GPR119c
> 0.417006618,KO,GPR119c
> 0.572958834,KO,GPR119c
> 0.229467444,KO,GPR119d
> 0.294089745,KO,GPR119d
> 0.309964445,KO,GPR119d
> 0.30474325,KO,GPR119e
> 0.159374839,KO,GPR119e
> 0.467726848,KO,GPR119e
> 1.01841912,KO,GPR119f
> 0.423028621,KO,GPR119f
> 0.223588597,KO,GPR119f
>
> Thank
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list