How to use tapply with more than one variables grouped

classic Classic list List threaded Threaded
23 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: How to use tapply with more than one variables grouped

arun kirshna
This post has NOT been accepted by the mailing list yet.
Hi,

I guess this is what you were looking for:
dat1<-read.table(text="
                  AL  AR  CA  NY
Doug    250 250 250  NA
Jennifer  20 340 300 100
Michele  250 500 250  60
Obama    15  45 520 600
",header=TRUE,stringsAsFactors=FALSE,sep="")
 res<-unlist(lapply(apply(dat1,2,function(x) x[!is.na(x)]),function(x) x[all(x["Obama"]>x[names(x)!=names(x)[grep("Obama",names(x))]])]))
res[grep("Obama",names(res))]
#CA.Obama NY.Obama
   #  520      600
Reply | Threaded
Open this post in threaded view
|

Re: How to use tapply with more than one variables grouped

David Winsemius
In reply to this post by noobmin

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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to use tapply with more than one variables grouped

arun kirshna


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"

>

> 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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
12