[R] How to use tapply with more than one variables grouped

David Winsemius dwinsemius at comcast.net
Wed Oct 24 02:45:16 CEST 2012


On Oct 23, 2012, at 1:25 PM, noobmin wrote:

>             AL  AR  CA  NY 
> Doug    250 250 250  NA 
> Jennifer  20 340 300 100 
> Michele  250 500 250  60 
> Obama    15  45 520 600 
> 
> My English is not very good, I'll try again. I want to list ALL states in
> the country where Obama had greater contribution. The table above shows the
> total contribution received by each candidate in a given state. To AL state
> obama not received more than Doug. For the AR state he received no more than
> others candidates. For the CA state he received a total of $ 520, which is
> 520>300>250>=250 and should be selected. In NY also had the largest
> contribution, $ 600, 600>100>60 and should therefore be selected.
> 
> I want to make it to the N presidency candidates and M states of the
> country. The table above is only an example.

Perhaps:

> apply(dat, 2, function(x) x["Obama"] == max(x, na.rm=TRUE) )
   AL    AR    CA    NY 
FALSE FALSE  TRUE  TRUE 

Or perhaps:
> names(dat)[ apply(dat, 2, function(x) x["Obama"] == max(x, na.rm=TRUE) ) ]
[1] "CA" "NY"

> 

> Sorry again, for me it was clear. = ( 
> Thanks
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-use-tapply-with-more-than-one-variables-grouped-tp4646948p4647220.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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
Alameda, CA, USA




More information about the R-help mailing list