Hi,
Just a modification of David's method:
apply(dat1,2,function(x) names(which.max(x[!is.na(x)]))=="Obama")
# AL AR CA NY
#FALSE FALSE TRUE TRUE
names(dat1)[apply(dat1,2,function(x) names(which.max(x[!is.na(x)]))=="Obama")]
#[1] "CA" "NY"
A.K.
----- Original Message -----
From: David Winsemius <
[hidden email]>
To: noobmin <
[hidden email]>
Cc:
[hidden email]
Sent: Tuesday, October 23, 2012 8:45 PM
Subject: Re: [R] How to use tapply with more than one variables grouped
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"
>
David Winsemius, MD
Alameda, CA, USA
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide
http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide
http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.