How to use tapply with more than one variables grouped
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 <dwinsemius at comcast.net> To: noobmin <pseudovoid at hotmail.com> Cc: r-help at r-project.org 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"
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 ______________________________________________ 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.