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

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


Hi,

Your question is not clear.

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

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="")

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)
names(res1)<-paste(names(res1),nam1,sep="_")
names(res1)<-gsub("\\d+","",names(res1))
res1
  #  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
res1[grep("Obama",names(res1))]
#Obama_CA Obama_NY 
  #   520      600 

A.K.



----- Original Message -----
From: noobmin <pseudovoid at hotmail.com>
To: r-help at r-project.org
Cc: 
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
contribution?




--
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
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.





More information about the R-help mailing list