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

arun smartpink111 at yahoo.com
Tue Oct 23 21:12:32 CEST 2012


Your question is not clear.

Suppose if you want to find the highest two contributions for each candidate:

                  AL  AR  CA  NY
Doug    250 250 250  NA
Jennifer  20 340 300 100
Michele  250 500 250  60
Obama    15  45 520 600

res1<-unlist(lapply(split(dat1,rownames(dat1)),function(x) tail(apply(x,1,sort),2)))
nam1<-unlist(lapply(lapply(split(dat1,rownames(dat1)),function(x) tail(apply(x,1,sort),2)),function(x) dimnames(x)[1]),use.names=F)
  #  Doug_AR     Doug_CA Jennifer_CA Jennifer_AR  Michele_CA  Michele_AR 
    #    250         250         300         340         250         500 
  # Obama_CA    Obama_NY 
    #    520         600 

#Contribution for Obama
#Obama_CA Obama_NY 
  #   520      600 


----- Original Message -----
From: noobmin <pseudovoid at hotmail.com>
To: r-help at r-project.org
Sent: Tuesday, October 23, 2012 12:48 PM
Subject: Re: [R] How to use tapply with more than one variables grouped

To take this example I reduced the number of records absurdly. In the
original database there are 48 000 candidates and dozens of states. There is
no way to analyze data visually. I would not put 400 mb of tables here. But
based on the example how could list the states where obama received more

View this message in context: http://r.789695.n4.nabble.com/How-to-use-tapply-with-more-than-one-variables-grouped-tp4646948p4647175.html
Sent from the R help mailing list archive at Nabble.com.

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list